|
| 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 | + |
| 7 | +#include "flutter/shell/platform/embedder/embedder.h" |
| 8 | + |
| 9 | +/** |
| 10 | + * FlEngine: |
| 11 | + * |
| 12 | + * #FlEngine is an abstract class that controls a FlutterEngine. |
| 13 | + */ |
| 14 | + |
| 15 | +typedef struct { |
| 16 | + FlDartProject* project; |
| 17 | + FLUTTER_API_SYMBOL(FlutterEngine) engine; |
| 18 | +} FlEnginePrivate; |
| 19 | + |
| 20 | +enum { PROP_FLUTTER_PROJECT = 1, PROP_LAST }; |
| 21 | + |
| 22 | +G_DEFINE_TYPE_WITH_PRIVATE(FlEngine, fl_engine, G_TYPE_OBJECT) |
| 23 | + |
| 24 | +static void* fl_engine_gl_proc_resolver(void* user_data, const char* name) { |
| 25 | + FlEngine* self = static_cast<FlEngine*>(user_data); |
| 26 | + |
| 27 | + return FL_ENGINE_GET_CLASS(self)->get_proc_address(self, name); |
| 28 | +} |
| 29 | + |
| 30 | +static bool fl_engine_gl_make_current(void* user_data) { |
| 31 | + FlEngine* self = static_cast<FlEngine*>(user_data); |
| 32 | + |
| 33 | + FL_ENGINE_GET_CLASS(self)->make_current(self); |
| 34 | + |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +static bool fl_engine_gl_clear_current(void* user_data) { |
| 39 | + FlEngine* self = static_cast<FlEngine*>(user_data); |
| 40 | + |
| 41 | + FL_ENGINE_GET_CLASS(self)->clear_current(self); |
| 42 | + |
| 43 | + return true; |
| 44 | +} |
| 45 | + |
| 46 | +static uint32_t fl_engine_gl_fbo_callback(void* user_data) { |
| 47 | + FlEngine* self = static_cast<FlEngine*>(user_data); |
| 48 | + |
| 49 | + return FL_ENGINE_GET_CLASS(self)->get_fbo(self); |
| 50 | +} |
| 51 | + |
| 52 | +static bool fl_engine_gl_present(void* user_data) { |
| 53 | + FlEngine* self = static_cast<FlEngine*>(user_data); |
| 54 | + |
| 55 | + FL_ENGINE_GET_CLASS(self)->present(self); |
| 56 | + |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +static void fl_engine_set_property(GObject* object, |
| 61 | + guint prop_id, |
| 62 | + const GValue* value, |
| 63 | + GParamSpec* pspec) { |
| 64 | + FlEngine* self = FL_ENGINE(object); |
| 65 | + FlEnginePrivate* priv = static_cast<FlEnginePrivate*>(fl_engine_get_instance_private(self)); |
| 66 | + |
| 67 | + switch (prop_id) { |
| 68 | + case PROP_FLUTTER_PROJECT: |
| 69 | + g_set_object(&priv->project, |
| 70 | + static_cast<FlDartProject*>(g_value_get_object(value))); |
| 71 | + break; |
| 72 | + default: |
| 73 | + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
| 74 | + break; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +static void fl_engine_get_property(GObject* object, |
| 79 | + guint prop_id, |
| 80 | + GValue* value, |
| 81 | + GParamSpec* pspec) { |
| 82 | + FlEngine* self = FL_ENGINE(object); |
| 83 | + FlEnginePrivate* priv = static_cast<FlEnginePrivate*>(fl_engine_get_instance_private(self)); |
| 84 | + |
| 85 | + switch (prop_id) { |
| 86 | + case PROP_FLUTTER_PROJECT: |
| 87 | + g_value_set_object(value, priv->project); |
| 88 | + break; |
| 89 | + default: |
| 90 | + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
| 91 | + break; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +static void fl_engine_dispose(GObject* object) { |
| 96 | + FlEngine* self = FL_ENGINE(object); |
| 97 | + FlEnginePrivate* priv = static_cast<FlEnginePrivate*>(fl_engine_get_instance_private(self)); |
| 98 | + |
| 99 | + FlutterEngineDeinitialize(priv->engine); |
| 100 | + FlutterEngineShutdown(priv->engine); |
| 101 | + |
| 102 | + g_clear_object(&priv->project); |
| 103 | + |
| 104 | + G_OBJECT_CLASS(fl_engine_parent_class)->dispose(object); |
| 105 | +} |
| 106 | + |
| 107 | +static gboolean fl_engine_real_start(FlEngine* self, GError **error) { |
| 108 | + FlEnginePrivate* priv = static_cast<FlEnginePrivate*>(fl_engine_get_instance_private(self)); |
| 109 | + |
| 110 | + g_return_val_if_fail(FL_IS_ENGINE(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_gl_proc_resolver; |
| 116 | + config.open_gl.make_current = fl_engine_gl_make_current; |
| 117 | + config.open_gl.clear_current = fl_engine_gl_clear_current; |
| 118 | + config.open_gl.fbo_callback = fl_engine_gl_fbo_callback; |
| 119 | + config.open_gl.present = fl_engine_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, "Failed to initialize Flutter engine"); |
| 130 | + return FALSE; |
| 131 | + } |
| 132 | + |
| 133 | + result = FlutterEngineRunInitialized(priv->engine); |
| 134 | + if (result != kSuccess) { |
| 135 | + g_set_error(error, fl_engine_error_quark(), FL_ENGINE_ERROR_FAILED, "Failed to run Flutter engine"); |
| 136 | + return FALSE; |
| 137 | + } |
| 138 | + |
| 139 | + return TRUE; |
| 140 | +} |
| 141 | + |
| 142 | +static void fl_engine_class_init(FlEngineClass* klass) { |
| 143 | + G_OBJECT_CLASS(klass)->set_property = fl_engine_set_property; |
| 144 | + G_OBJECT_CLASS(klass)->get_property = fl_engine_get_property; |
| 145 | + G_OBJECT_CLASS(klass)->dispose = fl_engine_dispose; |
| 146 | + klass->start = fl_engine_real_start; |
| 147 | + |
| 148 | + g_object_class_install_property( |
| 149 | + G_OBJECT_CLASS(klass), PROP_FLUTTER_PROJECT, |
| 150 | + g_param_spec_object( |
| 151 | + "flutter-project", "flutter-project", "Flutter project in use", |
| 152 | + fl_dart_project_get_type(), |
| 153 | + static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | |
| 154 | + G_PARAM_STATIC_STRINGS))); |
| 155 | +} |
| 156 | + |
| 157 | +static void fl_engine_init(FlEngine* self) {} |
| 158 | + |
| 159 | +G_DEFINE_QUARK(fl-engine-error-quark, fl_engine_error) |
| 160 | + |
| 161 | +FlDartProject* fl_engine_get_project(FlEngine* self) { |
| 162 | + FlEnginePrivate* priv = static_cast<FlEnginePrivate*>(fl_engine_get_instance_private(self)); |
| 163 | + |
| 164 | + g_return_val_if_fail(FL_IS_ENGINE(self), NULL); |
| 165 | + |
| 166 | + return priv->project; |
| 167 | +} |
| 168 | + |
| 169 | +gboolean fl_engine_start(FlEngine* self, GError **error) { |
| 170 | + return FL_ENGINE_GET_CLASS(self)->start(self, error); |
| 171 | +} |
| 172 | + |
| 173 | +void fl_engine_send_window_metrics_event(FlEngine* self, size_t width, size_t height, double pixel_ratio) { |
| 174 | + FlEnginePrivate* priv = static_cast<FlEnginePrivate*>(fl_engine_get_instance_private(self)); |
| 175 | + |
| 176 | + FlutterWindowMetricsEvent event = {}; |
| 177 | + event.struct_size = sizeof(FlutterWindowMetricsEvent); |
| 178 | + event.width = width; |
| 179 | + event.height = height; |
| 180 | + event.pixel_ratio = pixel_ratio; |
| 181 | + FlutterEngineSendWindowMetricsEvent(priv->engine, &event); |
| 182 | +} |
0 commit comments