Support AOT mode in GTK shell (#18809)

Simplify FlDartProject by removing the path arguments - we'll assume that it's
in the standard bundle layout and add new methods later if we need to support
other cases.
This commit is contained in:
Robert Ancell 2020-06-08 13:18:52 +12:00 committed by GitHub
parent 8516b39dcd
commit dfdd88deb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 109 additions and 148 deletions

View File

@ -13,7 +13,7 @@
// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new("data");
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) error = nullptr;

View File

@ -12,7 +12,7 @@
// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new("data");
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) error = nullptr;

View File

@ -9,119 +9,71 @@
struct _FlDartProject {
GObject parent_instance;
gchar* path;
gchar* aot_library_path;
gchar* assets_path;
gchar* icu_data_path;
};
enum { PROP_ASSETS_PATH = 1, PROP_ICU_DATA_PATH, PROP_PATH, PROP_LAST };
G_DEFINE_TYPE(FlDartProject, fl_dart_project, G_TYPE_OBJECT)
static void fl_dart_project_set_path(FlDartProject* self, const gchar* path) {
g_free(self->path);
if (g_path_is_absolute(path)) {
self->path = g_strdup(path);
} else {
g_autoptr(GError) error = nullptr;
g_autofree gchar* exe_path = g_file_read_link("/proc/self/exe", &error);
if (exe_path == nullptr) {
g_critical("Failed to determine location of executable: %s",
error->message);
}
g_autofree gchar* dir = g_path_get_dirname(exe_path);
self->path = g_build_filename(dir, path, nullptr);
// Gets the directory the current executable is in.
static gchar* get_executable_dir() {
g_autoptr(GError) error = nullptr;
g_autofree gchar* exe_path = g_file_read_link("/proc/self/exe", &error);
if (exe_path == nullptr) {
g_critical("Failed to determine location of executable: %s",
error->message);
return nullptr;
}
}
static void fl_dart_project_set_property(GObject* object,
guint prop_id,
const GValue* value,
GParamSpec* pspec) {
FlDartProject* self = FL_DART_PROJECT(object);
switch (prop_id) {
case PROP_PATH:
fl_dart_project_set_path(self, g_value_get_string(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void fl_dart_project_get_property(GObject* object,
guint prop_id,
GValue* value,
GParamSpec* pspec) {
FlDartProject* self = FL_DART_PROJECT(object);
switch (prop_id) {
case PROP_ASSETS_PATH:
g_value_take_string(value, fl_dart_project_get_assets_path(self));
break;
case PROP_ICU_DATA_PATH:
g_value_take_string(value, fl_dart_project_get_icu_data_path(self));
break;
case PROP_PATH:
g_value_set_string(value, self->path);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
return g_path_get_dirname(exe_path);
}
static void fl_dart_project_dispose(GObject* object) {
FlDartProject* self = FL_DART_PROJECT(object);
g_clear_pointer(&self->path, g_free);
g_clear_pointer(&self->aot_library_path, g_free);
g_clear_pointer(&self->assets_path, g_free);
g_clear_pointer(&self->icu_data_path, g_free);
G_OBJECT_CLASS(fl_dart_project_parent_class)->dispose(object);
}
static void fl_dart_project_class_init(FlDartProjectClass* klass) {
G_OBJECT_CLASS(klass)->set_property = fl_dart_project_set_property;
G_OBJECT_CLASS(klass)->get_property = fl_dart_project_get_property;
G_OBJECT_CLASS(klass)->dispose = fl_dart_project_dispose;
g_object_class_install_property(
G_OBJECT_CLASS(klass), PROP_ASSETS_PATH,
g_param_spec_string(
"assets-path", "assets-path", "Path to Flutter assets", nullptr,
static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
G_OBJECT_CLASS(klass), PROP_ICU_DATA_PATH,
g_param_spec_string(
"icu-data-path", "icu-data-path", "Path to ICU data", nullptr,
static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
G_OBJECT_CLASS(klass), PROP_PATH,
g_param_spec_string(
"path", "path", "Path to Flutter project", nullptr,
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS)));
}
static void fl_dart_project_init(FlDartProject* self) {}
G_MODULE_EXPORT FlDartProject* fl_dart_project_new(const gchar* path) {
return static_cast<FlDartProject*>(
g_object_new(fl_dart_project_get_type(), "path", path, nullptr));
G_MODULE_EXPORT FlDartProject* fl_dart_project_new() {
FlDartProject* self =
FL_DART_PROJECT(g_object_new(fl_dart_project_get_type(), nullptr));
g_autofree gchar* executable_dir = get_executable_dir();
self->aot_library_path =
g_build_filename(executable_dir, "lib", "libapp.so", nullptr);
self->assets_path =
g_build_filename(executable_dir, "data", "flutter_assets", nullptr);
self->icu_data_path =
g_build_filename(executable_dir, "data", "icudtl.dat", nullptr);
return self;
}
G_MODULE_EXPORT const gchar* fl_dart_project_get_path(FlDartProject* self) {
G_MODULE_EXPORT const gchar* fl_dart_project_get_aot_library_path(
FlDartProject* self) {
g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
return self->path;
return self->aot_library_path;
}
G_MODULE_EXPORT gchar* fl_dart_project_get_assets_path(FlDartProject* self) {
G_MODULE_EXPORT const gchar* fl_dart_project_get_assets_path(
FlDartProject* self) {
g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
return g_build_filename(self->path, "flutter_assets", nullptr);
return self->assets_path;
}
G_MODULE_EXPORT gchar* fl_dart_project_get_icu_data_path(FlDartProject* self) {
G_MODULE_EXPORT const gchar* fl_dart_project_get_icu_data_path(
FlDartProject* self) {
g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
return g_build_filename(self->path, "icudtl.dat", nullptr);
return self->icu_data_path;
}

View File

@ -7,31 +7,20 @@
#include <gmodule.h>
TEST(FlDartProjectTest, GetPath) {
g_autoptr(FlDartProject) project =
fl_dart_project_new("/projects/my_dart_project");
EXPECT_STREQ(fl_dart_project_get_path(project), "/projects/my_dart_project");
}
TEST(FlDartProjectTest, GetPathRelative) {
g_autoptr(FlDartProject) project = fl_dart_project_new("foo");
TEST(FlDartProjectTest, GetPaths) {
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autofree gchar* exe_path = g_file_read_link("/proc/self/exe", nullptr);
ASSERT_TRUE(exe_path != nullptr);
g_autofree gchar* dir = g_path_get_dirname(exe_path);
g_autofree gchar* expected_path = g_build_filename(dir, "foo", nullptr);
EXPECT_STREQ(fl_dart_project_get_path(project), expected_path);
}
TEST(FlDartProjectTest, GetAssetsPath) {
g_autoptr(FlDartProject) project =
fl_dart_project_new("/projects/my_dart_project");
g_autofree gchar* assets_path = fl_dart_project_get_assets_path(project);
EXPECT_STREQ(assets_path, "/projects/my_dart_project/flutter_assets");
}
TEST(FlDartProjectTest, GetIcuDataPath) {
g_autoptr(FlDartProject) project =
fl_dart_project_new("/projects/my_dart_project");
g_autofree gchar* assets_path = fl_dart_project_get_icu_data_path(project);
EXPECT_STREQ(assets_path, "/projects/my_dart_project/icudtl.dat");
g_autofree gchar* expected_aot_library_path =
g_build_filename(dir, "lib", "libapp.so", nullptr);
EXPECT_STREQ(fl_dart_project_get_aot_library_path(project),
expected_aot_library_path);
g_autofree gchar* expected_assets_path =
g_build_filename(dir, "data", "flutter_assets", nullptr);
EXPECT_STREQ(fl_dart_project_get_assets_path(project), expected_assets_path);
g_autofree gchar* expected_icu_data_path =
g_build_filename(dir, "data", "icudtl.dat", nullptr);
EXPECT_STREQ(fl_dart_project_get_icu_data_path(project),
expected_icu_data_path);
}

View File

@ -24,6 +24,7 @@ struct _FlEngine {
FlDartProject* project;
FlRenderer* renderer;
FlBinaryMessenger* binary_messenger;
FlutterEngineAOTData aot_data;
FLUTTER_API_SYMBOL(FlutterEngine) engine;
// Function to call when a platform message is received.
@ -162,7 +163,15 @@ static void fl_engine_platform_message_response_cb(const uint8_t* data,
static void fl_engine_dispose(GObject* object) {
FlEngine* self = FL_ENGINE(object);
FlutterEngineShutdown(self->engine);
if (self->engine != nullptr) {
FlutterEngineShutdown(self->engine);
self->engine = nullptr;
}
if (self->aot_data != nullptr) {
FlutterEngineCollectAOTData(self->aot_data);
self->aot_data = nullptr;
}
g_clear_object(&self->project);
g_clear_object(&self->renderer);
@ -232,6 +241,19 @@ gboolean fl_engine_start(FlEngine* self, GError** error) {
args.icu_data_path = fl_dart_project_get_icu_data_path(self->project);
args.platform_message_callback = fl_engine_platform_message_cb;
args.custom_task_runners = &custom_task_runners;
args.shutdown_dart_vm_when_done = true;
if (FlutterEngineRunsAOTCompiledDartCode()) {
FlutterEngineAOTDataSource source = {};
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
source.elf_path = fl_dart_project_get_aot_library_path(self->project);
if (FlutterEngineCreateAOTData(&source, &self->aot_data) != kSuccess) {
g_set_error(error, fl_engine_error_quark(), FL_ENGINE_ERROR_FAILED,
"Failed to create AOT data");
return FALSE;
}
args.aot_data = self->aot_data;
}
FlutterEngineResult result = FlutterEngineInitialize(
FLUTTER_ENGINE_VERSION, &config, &args, self, &self->engine);

View File

@ -15,7 +15,7 @@
// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new("data");
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) error = nullptr;

View File

@ -24,44 +24,27 @@ G_DECLARE_FINAL_TYPE(FlDartProject, fl_dart_project, FL, DART_PROJECT, GObject)
/**
* fl_dart_project_new:
* @path: a file path, e.g. "my_dart_project".
*
* Creates a Flutter project. The project path should contain the following
* top-level items:
* - icudtl.dat (provided as a resource by the Flutter tool)
* - flutter_assets (as built by the Flutter tool)
*
* The path can either be absolute, or relative to the directory containing the
* running executable.
* Creates a Flutter project for the currently running executable. The following
* data files are required relative to the location of the executable:
* - data/flutter_assets/ (as built by the Flutter tool).
* - data/icudtl.dat (provided as a resource by the Flutter tool).
* - lib/libapp.so (as built by the Flutter tool when in AOT mode).
*
* Returns: a new #FlDartProject.
*/
FlDartProject* fl_dart_project_new();
/**
* fl_dart_project_new:
* @path: (type filename): a file path, e.g. "my_dart_project".
*
* Creates a Flutter project. The project path should contain the following
* top-level items:
* - icudtl.dat (provided as a resource by the Flutter tool)
* - flutter_assets (as built by the Flutter tool)
*
* The path can either be absolute, or relative to the directory containing the
* running executable.
*
* Returns: a new #FlDartProject.
*/
FlDartProject* fl_dart_project_new(const gchar* path);
/**
* fl_dart_project_get_path:
* fl_dart_project_get_aot_library_path:
* @project: an #FlDartProject.
*
* Gets the path to the directory containing the Flutter application.
* Gets the path to the AOT library in the Flutter application.
*
* Returns: (type filename): a file path, e.g. "/projects/my_dart_project".
* Returns: (type filename): an absolute file path, e.g.
* "/projects/my_dart_project/lib/libapp.so".
*/
const gchar* fl_dart_project_get_path(FlDartProject* project);
const gchar* fl_dart_project_get_aot_library_path(FlDartProject* project);
/**
* fl_dart_project_get_assets_path:
@ -70,10 +53,10 @@ const gchar* fl_dart_project_get_path(FlDartProject* project);
* Gets the path to the directory containing the assets used in the Flutter
* application.
*
* Returns: (type filename): a file path, e.g.
* "/projects/my_dart_project/flutter_assets".
* Returns: (type filename): an absolute directory path, e.g.
* "/projects/my_dart_project/data/flutter_assets".
*/
gchar* fl_dart_project_get_assets_path(FlDartProject* project);
const gchar* fl_dart_project_get_assets_path(FlDartProject* project);
/**
* fl_dart_project_get_icu_data_path:
@ -81,10 +64,10 @@ gchar* fl_dart_project_get_assets_path(FlDartProject* project);
*
* Gets the path to the ICU data file in the Flutter application.
*
* Returns: (type filename): a file path, e.g.
* "/projects/my_dart_project/icudtl.dat".
* Returns: (type filename): an absolute file path, e.g.
* "/projects/my_dart_project/data/icudtl.dat".
*/
gchar* fl_dart_project_get_icu_data_path(FlDartProject* project);
const gchar* fl_dart_project_get_icu_data_path(FlDartProject* project);
G_END_DECLS

View File

@ -25,7 +25,7 @@ G_DECLARE_FINAL_TYPE(FlView, fl_view, FL, VIEW, GtkWidget)
*
* The following example shows how to set up a view in a GTK application:
* |[<!-- language="C" -->
* FlDartProject *project = fl_dart_project_new ("data");
* FlDartProject *project = fl_dart_project_new ();
* FlView *view = fl_view_new (project);
* gtk_widget_show (GTK_WIDGET (view));
* gtk_container_add (GTK_CONTAINER (parent), view);

View File

@ -122,6 +122,17 @@ static void invoke_method(FLUTTER_API_SYMBOL(FlutterEngine) engine,
engine->platform_post_task_callback(task, 0, engine->user_data);
}
FlutterEngineResult FlutterEngineCreateAOTData(
const FlutterEngineAOTDataSource* source,
FlutterEngineAOTData* data_out) {
*data_out = nullptr;
return kSuccess;
}
FlutterEngineResult FlutterEngineCollectAOTData(FlutterEngineAOTData data) {
return kSuccess;
}
FlutterEngineResult FlutterEngineRun(size_t version,
const FlutterRendererConfig* config,
const FlutterProjectArgs* args,
@ -367,3 +378,7 @@ FlutterEngineResult FlutterEngineRunTask(FLUTTER_API_SYMBOL(FlutterEngine)
return kSuccess;
}
bool FlutterEngineRunsAOTCompiledDartCode() {
return false;
}