|
| 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 | +} |
0 commit comments