|
| 1 | +// Copyright 2018 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_application.h" |
| 6 | + |
| 7 | +#include <sys/types.h> |
| 8 | + |
| 9 | +#include <chrono> |
| 10 | +#include <sstream> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "utils.h" |
| 14 | + |
| 15 | +namespace flutter { |
| 16 | + |
| 17 | +static_assert(FLUTTER_ENGINE_VERSION == 1, ""); |
| 18 | + |
| 19 | +static const char *kICUDataFileName = "icudtl.dat"; |
| 20 | + |
| 21 | +static std::string GetICUDataPath() { |
| 22 | + auto exe_dir = GetExecutableDirectory(); |
| 23 | + if (exe_dir == "") { |
| 24 | + return ""; |
| 25 | + } |
| 26 | + std::stringstream stream; |
| 27 | + stream << exe_dir << kICUDataFileName; |
| 28 | + |
| 29 | + auto icu_path = stream.str(); |
| 30 | + |
| 31 | + if (!FileExistsAtPath(icu_path.c_str())) { |
| 32 | + FLWAY_ERROR << "Could not find " << icu_path << std::endl; |
| 33 | + return ""; |
| 34 | + } |
| 35 | + |
| 36 | + return icu_path; |
| 37 | +} |
| 38 | + |
| 39 | +FlutterApplication::FlutterApplication( |
| 40 | + std::string bundle_path, const std::vector<std::string> &command_line_args, |
| 41 | + RenderDelegate &render_delegate) |
| 42 | + : render_delegate_(render_delegate) { |
| 43 | + if (!FlutterAssetBundleIsValid(bundle_path)) { |
| 44 | + FLWAY_ERROR << "Flutter asset bundle was not valid." << std::endl; |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + FlutterRendererConfig config = {}; |
| 49 | + config.type = kOpenGL; |
| 50 | + config.open_gl.struct_size = sizeof(config.open_gl); |
| 51 | + config.open_gl.make_current = [](void *userdata) -> bool { |
| 52 | + return reinterpret_cast<FlutterApplication *>(userdata) |
| 53 | + ->render_delegate_.OnApplicationContextMakeCurrent(); |
| 54 | + }; |
| 55 | + config.open_gl.clear_current = [](void *userdata) -> bool { |
| 56 | + return reinterpret_cast<FlutterApplication *>(userdata) |
| 57 | + ->render_delegate_.OnApplicationContextClearCurrent(); |
| 58 | + }; |
| 59 | + config.open_gl.present = [](void *userdata) -> bool { |
| 60 | + return reinterpret_cast<FlutterApplication *>(userdata) |
| 61 | + ->render_delegate_.OnApplicationPresent(); |
| 62 | + }; |
| 63 | + config.open_gl.fbo_callback = [](void *userdata) -> uint32_t { |
| 64 | + return reinterpret_cast<FlutterApplication *>(userdata) |
| 65 | + ->render_delegate_.OnApplicationGetOnscreenFBO(); |
| 66 | + }; |
| 67 | + config.open_gl.gl_proc_resolver = |
| 68 | + [&render_delegate_](void *userdata, const char *name) -> void * { |
| 69 | + return reinterpret_cast<FlutterApplication *>(userdata) |
| 70 | + ->render_delegate_.GetProcAddress(name); |
| 71 | + }; |
| 72 | + |
| 73 | + auto icu_data_path = GetICUDataPath(); |
| 74 | + |
| 75 | + if (icu_data_path == "") { |
| 76 | + FLWAY_ERROR << "Could not find ICU data. It should be placed next to the " |
| 77 | + "executable but it wasn't there." |
| 78 | + << std::endl; |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + std::vector<const char *> command_line_args_c; |
| 83 | + |
| 84 | + for (const auto &arg : command_line_args) { |
| 85 | + command_line_args_c.push_back(arg.c_str()); |
| 86 | + } |
| 87 | + |
| 88 | + FlutterProjectArgs args = { |
| 89 | + .struct_size = sizeof(FlutterProjectArgs), |
| 90 | + .assets_path = bundle_path.c_str(), |
| 91 | + .main_path = "", |
| 92 | + .packages_path = "", |
| 93 | + .icu_data_path = icu_data_path.c_str(), |
| 94 | + .command_line_argc = static_cast<int>(command_line_args_c.size()), |
| 95 | + .command_line_argv = command_line_args_c.data(), |
| 96 | + }; |
| 97 | + |
| 98 | + FlutterEngine engine = nullptr; |
| 99 | + auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args, |
| 100 | + this /* userdata */, &engine_); |
| 101 | + |
| 102 | + if (result != kSuccess) { |
| 103 | + FLWAY_ERROR << "Could not run the Flutter engine" << std::endl; |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + valid_ = true; |
| 108 | +} |
| 109 | + |
| 110 | +FlutterApplication::~FlutterApplication() { |
| 111 | + if (engine_ == nullptr) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + auto result = FlutterEngineShutdown(engine_); |
| 116 | + |
| 117 | + if (result != kSuccess) { |
| 118 | + FLWAY_ERROR << "Could not shutdown the Flutter engine." << std::endl; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +bool FlutterApplication::IsValid() const { return valid_; } |
| 123 | + |
| 124 | +bool FlutterApplication::SetWindowSize(size_t width, size_t height) { |
| 125 | + FlutterWindowMetricsEvent event = {}; |
| 126 | + event.struct_size = sizeof(event); |
| 127 | + event.width = width; |
| 128 | + event.height = height; |
| 129 | + event.pixel_ratio = 1.0; |
| 130 | + return FlutterEngineSendWindowMetricsEvent(engine_, &event) == kSuccess; |
| 131 | +} |
| 132 | + |
| 133 | +void FlutterApplication::ProcessEvents() { |
| 134 | + __FlutterEngineFlushPendingTasksNow(); |
| 135 | +} |
| 136 | + |
| 137 | +bool FlutterApplication::SendPointerEvent(int button, int x, int y) { |
| 138 | + if (!valid_) { |
| 139 | + FLWAY_ERROR << "Pointer events on an invalid application." << std::endl; |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + // Simple hover event. Nothing to do. |
| 144 | + if (last_button_ == 0 && button == 0) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + FlutterPointerPhase phase = kCancel; |
| 149 | + |
| 150 | + if (last_button_ == 0 && button != 0) { |
| 151 | + phase = kDown; |
| 152 | + } else if (last_button_ == button) { |
| 153 | + phase = kMove; |
| 154 | + } else { |
| 155 | + phase = kUp; |
| 156 | + } |
| 157 | + |
| 158 | + last_button_ = button; |
| 159 | + return SendFlutterPointerEvent(phase, x, y); |
| 160 | +} |
| 161 | + |
| 162 | +bool FlutterApplication::SendFlutterPointerEvent(FlutterPointerPhase phase, |
| 163 | + double x, double y) { |
| 164 | + FlutterPointerEvent event = {}; |
| 165 | + event.struct_size = sizeof(event); |
| 166 | + event.phase = phase; |
| 167 | + event.x = x; |
| 168 | + event.y = y; |
| 169 | + event.timestamp = |
| 170 | + std::chrono::duration_cast<std::chrono::microseconds>( |
| 171 | + std::chrono::high_resolution_clock::now().time_since_epoch()) |
| 172 | + .count(); |
| 173 | + return FlutterEngineSendPointerEvent(engine_, &event, 1) == kSuccess; |
| 174 | +} |
| 175 | + |
| 176 | +void FlutterApplication::ReadInputEvents() { |
| 177 | + // TODO(chinmaygarde): Fill this in for touch screen and not just devices that |
| 178 | + // fake mice. |
| 179 | + ::sleep(INT_MAX); |
| 180 | +} |
| 181 | + |
| 182 | +} // namespace flutter |
0 commit comments