| 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 SHELL_COMMON_SHELL_H_ |
| 6 | #define SHELL_COMMON_SHELL_H_ |
| 7 | |
| 8 | #include <functional> |
| 9 | #include <mutex> |
| 10 | #include <string_view> |
| 11 | #include <unordered_map> |
| 12 | |
| 13 | #include "flutter/assets/directory_asset_bundle.h" |
| 14 | #include "flutter/common/graphics/texture.h" |
| 15 | #include "flutter/common/settings.h" |
| 16 | #include "flutter/common/task_runners.h" |
| 17 | #include "flutter/flow/surface.h" |
| 18 | #include "flutter/fml/closure.h" |
| 19 | #include "flutter/fml/macros.h" |
| 20 | #include "flutter/fml/memory/ref_ptr.h" |
| 21 | #include "flutter/fml/memory/thread_checker.h" |
| 22 | #include "flutter/fml/memory/weak_ptr.h" |
| 23 | #include "flutter/fml/status.h" |
| 24 | #include "flutter/fml/synchronization/sync_switch.h" |
| 25 | #include "flutter/fml/synchronization/waitable_event.h" |
| 26 | #include "flutter/fml/thread.h" |
| 27 | #include "flutter/fml/time/time_point.h" |
| 28 | #include "flutter/lib/ui/painting/image_generator_registry.h" |
| 29 | #include "flutter/lib/ui/semantics/custom_accessibility_action.h" |
| 30 | #include "flutter/lib/ui/semantics/semantics_node.h" |
| 31 | #include "flutter/lib/ui/volatile_path_tracker.h" |
| 32 | #include "flutter/lib/ui/window/platform_message.h" |
| 33 | #include "flutter/runtime/dart_vm_lifecycle.h" |
| 34 | #include "flutter/runtime/platform_data.h" |
| 35 | #include "flutter/runtime/service_protocol.h" |
| 36 | #include "flutter/shell/common/animator.h" |
| 37 | #include "flutter/shell/common/display_manager.h" |
| 38 | #include "flutter/shell/common/engine.h" |
| 39 | #include "flutter/shell/common/platform_view.h" |
| 40 | #include "flutter/shell/common/rasterizer.h" |
| 41 | #include "flutter/shell/common/resource_cache_limit_calculator.h" |
| 42 | #include "flutter/shell/common/shell_io_manager.h" |
| 43 | |
| 44 | namespace flutter { |
| 45 | |
| 46 | /// Error exit codes for the Dart isolate. |
| 47 | enum class DartErrorCode { |
| 48 | /// No error has occurred. |
| 49 | NoError = 0, |
| 50 | /// The Dart error code for an API error. |
| 51 | ApiError = 253, |
| 52 | /// The Dart error code for a compilation error. |
| 53 | CompilationError = 254, |
| 54 | /// The Dart error code for an unknown error. |
| 55 | UnknownError = 255 |
| 56 | }; |
| 57 | |
| 58 | /// Values for |Shell::SetGpuAvailability|. |
| 59 | enum class GpuAvailability { |
| 60 | /// Indicates that GPU operations should be permitted. |
| 61 | kAvailable = 0, |
| 62 | /// Indicates that the GPU is about to become unavailable, and to attempt to |
| 63 | /// flush any GPU related resources now. |
| 64 | kFlushAndMakeUnavailable = 1, |
| 65 | /// Indicates that the GPU is unavailable, and that no attempt should be made |
| 66 | /// to even flush GPU objects until it is available again. |
| 67 | kUnavailable = 2 |
| 68 | }; |
| 69 | |
| 70 | //------------------------------------------------------------------------------ |
| 71 | /// Perhaps the single most important class in the Flutter engine repository. |
| 72 | /// When embedders create a Flutter application, they are referring to the |
| 73 | /// creation of an instance of a shell. Creation and destruction of the shell is |
| 74 | /// synchronous and the embedder only holds a unique pointer to the shell. The |
| 75 | /// shell does not create the threads its primary components run on. Instead, it |
| 76 | /// is the embedder's responsibility to create threads and give the shell task |
| 77 | /// runners for those threads. Due to deterministic destruction of the shell, |
| 78 | /// the embedder can terminate all threads immediately after collecting the |
| 79 | /// shell. The shell must be created and destroyed on the same thread, but, |
| 80 | /// different shells (i.e. a separate instances of a Flutter application) may be |
| 81 | /// run on different threads simultaneously. The task runners themselves do not |
| 82 | /// have to be unique. If all task runner references given to the shell during |
| 83 | /// shell creation point to the same task runner, the Flutter application is |
| 84 | /// effectively single threaded. |
| 85 | /// |
| 86 | /// The shell is the central nervous system of the Flutter application. None of |
| 87 | /// the shell components are thread safe and must be created, accessed and |
| 88 | /// destroyed on the same thread. To interact with one another, the various |
| 89 | /// components delegate to the shell for communication. Instead of using back |
| 90 | /// pointers to the shell, a delegation pattern is used by all components that |
| 91 | /// want to communicate with one another. Because of this, the shell implements |
| 92 | /// the delegate interface for all these components. |
| 93 | /// |
| 94 | /// All shell methods accessed by the embedder may only be called on the |
| 95 | /// platform task runner. In case the embedder wants to directly access a shell |
| 96 | /// subcomponent, it is the embedder's responsibility to acquire a weak pointer |
| 97 | /// to that component and post a task to the task runner used by the component |
| 98 | /// to access its methods. The shell must also be destroyed on the platform |
| 99 | /// task runner. |
| 100 | /// |
| 101 | /// There is no explicit API to bootstrap and shutdown the Dart VM. The first |
| 102 | /// instance of the shell in the process bootstraps the Dart VM and the |
| 103 | /// destruction of the last shell instance destroys the same. Since different |
| 104 | /// shells may be created and destroyed on different threads. VM bootstrap may |
| 105 | /// happen on one thread but its collection on another. This behavior is thread |
| 106 | /// safe. |
| 107 | /// |
| 108 | class Shell final : public PlatformView::Delegate, |
| 109 | public Animator::Delegate, |
| 110 | public Engine::Delegate, |
| 111 | public Rasterizer::Delegate, |
| 112 | public ServiceProtocol::Handler, |
| 113 | public ResourceCacheLimitItem { |
| 114 | public: |
| 115 | template <class T> |
| 116 | using CreateCallback = std::function<std::unique_ptr<T>(Shell&)>; |
| 117 | typedef std::function<std::unique_ptr<Engine>( |
| 118 | Engine::Delegate& delegate, |
| 119 | const PointerDataDispatcherMaker& dispatcher_maker, |
| 120 | DartVM& vm, |
| 121 | fml::RefPtr<const DartSnapshot> isolate_snapshot, |
| 122 | TaskRunners task_runners, |
| 123 | const PlatformData& platform_data, |
| 124 | Settings settings, |
| 125 | std::unique_ptr<Animator> animator, |
| 126 | fml::WeakPtr<IOManager> io_manager, |
| 127 | fml::RefPtr<SkiaUnrefQueue> unref_queue, |
| 128 | fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate, |
| 129 | std::shared_ptr<VolatilePathTracker> volatile_path_tracker, |
| 130 | const std::shared_ptr<fml::SyncSwitch>& gpu_disabled_switch)> |
| 131 | EngineCreateCallback; |
| 132 | |
| 133 | //---------------------------------------------------------------------------- |
| 134 | /// @brief Creates a shell instance using the provided settings. The |
| 135 | /// callbacks to create the various shell subcomponents will be |
| 136 | /// called on the appropriate threads before this method returns. |
| 137 | /// If this is the first instance of a shell in the process, this |
| 138 | /// call also bootstraps the Dart VM. |
| 139 | /// @note The root isolate which will run this Shell's Dart code takes |
| 140 | /// its instructions from the passed in settings. This allows |
| 141 | /// embedders to host multiple Shells with different Dart code. |
| 142 | /// |
| 143 | /// @param[in] task_runners The task runners |
| 144 | /// @param[in] settings The settings |
| 145 | /// @param[in] on_create_platform_view The callback that must return a |
| 146 | /// platform view. This will be called on |
| 147 | /// the platform task runner before this |
| 148 | /// method returns. |
| 149 | /// @param[in] on_create_rasterizer That callback that must provide a |
| 150 | /// valid rasterizer. This will be called |
| 151 | /// on the render task runner before this |
| 152 | /// method returns. |
| 153 | /// @param[in] is_gpu_disabled The default value for the switch that |
| 154 | /// turns off the GPU. |
| 155 | /// |
| 156 | /// @return A full initialized shell if the settings and callbacks are |
| 157 | /// valid. The root isolate has been created but not yet launched. |
| 158 | /// It may be launched by obtaining the engine weak pointer and |
| 159 | /// posting a task onto the UI task runner with a valid run |
| 160 | /// configuration to run the isolate. The embedder must always |
| 161 | /// check the validity of the shell (using the IsSetup call) |
| 162 | /// immediately after getting a pointer to it. |
| 163 | /// |
| 164 | static std::unique_ptr<Shell> Create( |
| 165 | const PlatformData& platform_data, |
| 166 | const TaskRunners& task_runners, |
| 167 | Settings settings, |
| 168 | const CreateCallback<PlatformView>& on_create_platform_view, |
| 169 | const CreateCallback<Rasterizer>& on_create_rasterizer, |
| 170 | bool is_gpu_disabled = false); |
| 171 | |
| 172 | //---------------------------------------------------------------------------- |
| 173 | /// @brief Destroys the shell. This is a synchronous operation and |
| 174 | /// synchronous barrier blocks are introduced on the various |
| 175 | /// threads to ensure shutdown of all shell sub-components before |
| 176 | /// this method returns. |
| 177 | /// |
| 178 | ~Shell(); |
| 179 | |
| 180 | //---------------------------------------------------------------------------- |
| 181 | /// @brief Creates one Shell from another Shell where the created Shell |
| 182 | /// takes the opportunity to share any internal components it can. |
| 183 | /// This results is a Shell that has a smaller startup time cost |
| 184 | /// and a smaller memory footprint than an Shell created with the |
| 185 | /// Create function. |
| 186 | /// |
| 187 | /// The new Shell is returned in a running state so RunEngine |
| 188 | /// shouldn't be called again on the Shell. Once running, the |
| 189 | /// second Shell is mostly independent from the original Shell |
| 190 | /// and the original Shell doesn't need to keep running for the |
| 191 | /// spawned Shell to keep functioning. |
| 192 | /// @param[in] run_configuration A RunConfiguration used to run the Isolate |
| 193 | /// associated with this new Shell. It doesn't have to be the same |
| 194 | /// configuration as the current Shell but it needs to be in the |
| 195 | /// same snapshot or AOT. |
| 196 | /// |
| 197 | /// @see http://flutter.dev/go/multiple-engines |
| 198 | std::unique_ptr<Shell> Spawn( |
| 199 | RunConfiguration run_configuration, |
| 200 | const std::string& initial_route, |
| 201 | const CreateCallback<PlatformView>& on_create_platform_view, |
| 202 | const CreateCallback<Rasterizer>& on_create_rasterizer) const; |
| 203 | |
| 204 | //---------------------------------------------------------------------------- |
| 205 | /// @brief Starts an isolate for the given RunConfiguration. |
| 206 | /// |
| 207 | void RunEngine(RunConfiguration run_configuration); |
| 208 | |
| 209 | //---------------------------------------------------------------------------- |
| 210 | /// @brief Starts an isolate for the given RunConfiguration. The |
| 211 | /// result_callback will be called with the status of the |
| 212 | /// operation. |
| 213 | /// |
| 214 | void RunEngine(RunConfiguration run_configuration, |
| 215 | const std::function<void(Engine::RunStatus)>& result_callback); |
| 216 | |
| 217 | //------------------------------------------------------------------------------ |
| 218 | /// @return The settings used to launch this shell. |
| 219 | /// |
| 220 | const Settings& GetSettings() const override; |
| 221 | |
| 222 | //------------------------------------------------------------------------------ |
| 223 | /// @brief If callers wish to interact directly with any shell |
| 224 | /// subcomponents, they must (on the platform thread) obtain a |
| 225 | /// task runner that the component is designed to run on and a |
| 226 | /// weak pointer to that component. They may then post a task to |
| 227 | /// that task runner, do the validity check on that task runner |
| 228 | /// before performing any operation on that component. This |
| 229 | /// accessor allows callers to access the task runners for this |
| 230 | /// shell. |
| 231 | /// |
| 232 | /// @return The task runners current in use by the shell. |
| 233 | /// |
| 234 | const TaskRunners& GetTaskRunners() const override; |
| 235 | |
| 236 | //------------------------------------------------------------------------------ |
| 237 | /// @brief Getting the raster thread merger from parent shell, it can be |
| 238 | /// a null RefPtr when it's a root Shell or the |
| 239 | /// embedder_->SupportsDynamicThreadMerging() returns false. |
| 240 | /// |
| 241 | /// @return The raster thread merger used by the parent shell. |
| 242 | /// |
| 243 | const fml::RefPtr<fml::RasterThreadMerger> GetParentRasterThreadMerger() |
| 244 | const override; |
| 245 | |
| 246 | //---------------------------------------------------------------------------- |
| 247 | /// @brief Rasterizers may only be accessed on the raster task runner. |
| 248 | /// |
| 249 | /// @return A weak pointer to the rasterizer. |
| 250 | /// |
| 251 | fml::TaskRunnerAffineWeakPtr<Rasterizer> GetRasterizer() const; |
| 252 | |
| 253 | //------------------------------------------------------------------------------ |
| 254 | /// @brief Engines may only be accessed on the UI thread. This method is |
| 255 | /// deprecated, and implementers should instead use other API |
| 256 | /// available on the Shell or the PlatformView. |
| 257 | /// |
| 258 | /// @return A weak pointer to the engine. |
| 259 | /// |
| 260 | fml::WeakPtr<Engine> GetEngine(); |
| 261 | |
| 262 | //---------------------------------------------------------------------------- |
| 263 | /// @brief Platform views may only be accessed on the platform task |
| 264 | /// runner. |
| 265 | /// |
| 266 | /// @return A weak pointer to the platform view. |
| 267 | /// |
| 268 | fml::WeakPtr<PlatformView> GetPlatformView(); |
| 269 | |
| 270 | //---------------------------------------------------------------------------- |
| 271 | /// @brief The IO Manager may only be accessed on the IO task runner. |
| 272 | /// |
| 273 | /// @return A weak pointer to the IO manager. |
| 274 | /// |
| 275 | fml::WeakPtr<ShellIOManager> GetIOManager(); |
| 276 | |
| 277 | // Embedders should call this under low memory conditions to free up |
| 278 | // internal caches used. |
| 279 | // |
| 280 | // This method posts a task to the raster threads to signal the Rasterizer to |
| 281 | // free resources. |
| 282 | |
| 283 | //---------------------------------------------------------------------------- |
| 284 | /// @brief Used by embedders to notify that there is a low memory |
| 285 | /// warning. The shell will attempt to purge caches. Current, only |
| 286 | /// the rasterizer cache is purged. |
| 287 | void NotifyLowMemoryWarning() const; |
| 288 | |
| 289 | //---------------------------------------------------------------------------- |
| 290 | /// @brief Used by embedders to check if all shell subcomponents are |
| 291 | /// initialized. It is the embedder's responsibility to make this |
| 292 | /// call before accessing any other shell method. A shell that is |
| 293 | /// not set up must be discarded and another one created with |
| 294 | /// updated settings. |
| 295 | /// |
| 296 | /// @return Returns if the shell has been set up. Once set up, this does |
| 297 | /// not change for the life-cycle of the shell. |
| 298 | /// |
| 299 | bool IsSetup() const; |
| 300 | |
| 301 | //---------------------------------------------------------------------------- |
| 302 | /// @brief Captures a screenshot and optionally Base64 encodes the data |
| 303 | /// of the last layer tree rendered by the rasterizer in this |
| 304 | /// shell. |
| 305 | /// |
| 306 | /// @param[in] type The type of screenshot to capture. |
| 307 | /// @param[in] base64_encode If the screenshot data should be base64 |
| 308 | /// encoded. |
| 309 | /// |
| 310 | /// @return The screenshot result. |
| 311 | /// |
| 312 | Rasterizer::Screenshot Screenshot(Rasterizer::ScreenshotType type, |
| 313 | bool base64_encode); |
| 314 | |
| 315 | //---------------------------------------------------------------------------- |
| 316 | /// @brief Pauses the calling thread until the first frame is presented. |
| 317 | /// |
| 318 | /// @param[in] timeout The duration to wait before timing out. If this |
| 319 | /// duration would cause an overflow when added to |
| 320 | /// std::chrono::steady_clock::now(), this method will |
| 321 | /// wait indefinitely for the first frame. |
| 322 | /// |
| 323 | /// @return 'kOk' when the first frame has been presented before the |
| 324 | /// timeout successfully, 'kFailedPrecondition' if called from the |
| 325 | /// GPU or UI thread, 'kDeadlineExceeded' if there is a timeout. |
| 326 | /// |
| 327 | fml::Status WaitForFirstFrame(fml::TimeDelta timeout); |
| 328 | |
| 329 | //---------------------------------------------------------------------------- |
| 330 | /// @brief Used by embedders to reload the system fonts in |
| 331 | /// FontCollection. |
| 332 | /// It also clears the cached font families and send system |
| 333 | /// channel message to framework to rebuild affected widgets. |
| 334 | /// |
| 335 | /// @return Returns if shell reloads system fonts successfully. |
| 336 | /// |
| 337 | bool ReloadSystemFonts(); |
| 338 | |
| 339 | //---------------------------------------------------------------------------- |
| 340 | /// @brief Used by embedders to get the last error from the Dart UI |
| 341 | /// Isolate, if one exists. |
| 342 | /// |
| 343 | /// @return Returns the last error code from the UI Isolate. |
| 344 | /// |
| 345 | std::optional<DartErrorCode> GetUIIsolateLastError() const; |
| 346 | |
| 347 | //---------------------------------------------------------------------------- |
| 348 | /// @brief Used by embedders to check if the Engine is running and has |
| 349 | /// any live ports remaining. For example, the Flutter tester uses |
| 350 | /// this method to check whether it should continue to wait for |
| 351 | /// a running test or not. |
| 352 | /// |
| 353 | /// @return Returns if the shell has an engine and the engine has any live |
| 354 | /// Dart ports. |
| 355 | /// |
| 356 | bool EngineHasLivePorts() const; |
| 357 | |
| 358 | //---------------------------------------------------------------------------- |
| 359 | /// @brief Accessor for the disable GPU SyncSwitch. |
| 360 | // |Rasterizer::Delegate| |
| 361 | std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() |
| 362 | const override; |
| 363 | |
| 364 | //---------------------------------------------------------------------------- |
| 365 | /// @brief Marks the GPU as available or unavailable. |
| 366 | void SetGpuAvailability(GpuAvailability availability); |
| 367 | |
| 368 | //---------------------------------------------------------------------------- |
| 369 | /// @brief Get a pointer to the Dart VM used by this running shell |
| 370 | /// instance. |
| 371 | /// |
| 372 | /// @return The Dart VM pointer. |
| 373 | /// |
| 374 | DartVM* GetDartVM(); |
| 375 | |
| 376 | //---------------------------------------------------------------------------- |
| 377 | /// @brief Notifies the display manager of the updates. |
| 378 | /// |
| 379 | void OnDisplayUpdates(std::vector<std::unique_ptr<Display>> displays); |
| 380 | |
| 381 | //---------------------------------------------------------------------------- |
| 382 | /// @brief Queries the `DisplayManager` for the main display refresh rate. |
| 383 | /// |
| 384 | double GetMainDisplayRefreshRate(); |
| 385 | |
| 386 | //---------------------------------------------------------------------------- |
| 387 | /// @brief Install a new factory that can match against and decode image |
| 388 | /// data. |
| 389 | /// @param[in] factory Callback that produces `ImageGenerator`s for |
| 390 | /// compatible input data. |
| 391 | /// @param[in] priority The priority used to determine the order in which |
| 392 | /// factories are tried. Higher values mean higher |
| 393 | /// priority. The built-in Skia decoders are installed |
| 394 | /// at priority 0, and so a priority > 0 takes precedent |
| 395 | /// over the builtin decoders. When multiple decoders |
| 396 | /// are added with the same priority, those which are |
| 397 | /// added earlier take precedent. |
| 398 | /// @see `CreateCompatibleGenerator` |
| 399 | void RegisterImageDecoder(ImageGeneratorFactory factory, int32_t priority); |
| 400 | |
| 401 | // |Engine::Delegate| |
| 402 | const std::shared_ptr<PlatformMessageHandler>& GetPlatformMessageHandler() |
| 403 | const override; |
| 404 | |
| 405 | const std::weak_ptr<VsyncWaiter> GetVsyncWaiter() const; |
| 406 | |
| 407 | const std::shared_ptr<fml::ConcurrentTaskRunner> |
| 408 | GetConcurrentWorkerTaskRunner() const; |
| 409 | |
| 410 | private: |
| 411 | using ServiceProtocolHandler = |
| 412 | std::function<bool(const ServiceProtocol::Handler::ServiceProtocolMap&, |
| 413 | rapidjson::Document*)>; |
| 414 | |
| 415 | /// A collection of message channels (by name) that have sent at least one |
| 416 | /// message from a non-platform thread. Used to prevent printing the error log |
| 417 | /// more than once per channel, as a badly behaving plugin may send multiple |
| 418 | /// messages per second indefinitely. |
| 419 | std::mutex misbehaving_message_channels_mutex_; |
| 420 | std::set<std::string> misbehaving_message_channels_; |
| 421 | const TaskRunners task_runners_; |
| 422 | const fml::RefPtr<fml::RasterThreadMerger> parent_raster_thread_merger_; |
| 423 | std::shared_ptr<ResourceCacheLimitCalculator> |
| 424 | resource_cache_limit_calculator_; |
| 425 | size_t resource_cache_limit_; |
| 426 | const Settings settings_; |
| 427 | DartVMRef vm_; |
| 428 | mutable std::mutex time_recorder_mutex_; |
| 429 | std::optional<fml::TimePoint> latest_frame_target_time_; |
| 430 | std::unique_ptr<PlatformView> platform_view_; // on platform task runner |
| 431 | std::unique_ptr<Engine> engine_; // on UI task runner |
| 432 | std::unique_ptr<Rasterizer> rasterizer_; // on raster task runner |
| 433 | std::shared_ptr<ShellIOManager> io_manager_; // on IO task runner |
| 434 | std::shared_ptr<fml::SyncSwitch> is_gpu_disabled_sync_switch_; |
| 435 | std::shared_ptr<VolatilePathTracker> volatile_path_tracker_; |
| 436 | std::shared_ptr<PlatformMessageHandler> platform_message_handler_; |
| 437 | std::atomic<bool> route_messages_through_platform_thread_ = false; |
| 438 | |
| 439 | fml::WeakPtr<Engine> weak_engine_; // to be shared across threads |
| 440 | fml::TaskRunnerAffineWeakPtr<Rasterizer> |
| 441 | weak_rasterizer_; // to be shared across threads |
| 442 | fml::WeakPtr<PlatformView> |
| 443 | weak_platform_view_; // to be shared across threads |
| 444 | |
| 445 | std::unordered_map<std::string_view, // method |
| 446 | std::pair<fml::RefPtr<fml::TaskRunner>, |
| 447 | ServiceProtocolHandler> // task-runner/function |
| 448 | // pair |
| 449 | > |
| 450 | service_protocol_handlers_; |
| 451 | bool is_set_up_ = false; |
| 452 | bool is_added_to_service_protocol_ = false; |
| 453 | uint64_t next_pointer_flow_id_ = 0; |
| 454 | |
| 455 | bool first_frame_rasterized_ = false; |
| 456 | std::atomic<bool> waiting_for_first_frame_ = true; |
| 457 | std::mutex waiting_for_first_frame_mutex_; |
| 458 | std::condition_variable waiting_for_first_frame_condition_; |
| 459 | |
| 460 | // Written in the UI thread and read from the raster thread. Hence make it |
| 461 | // atomic. |
| 462 | std::atomic<bool> needs_report_timings_{false}; |
| 463 | |
| 464 | // Whether there's a task scheduled to report the timings to Dart through |
| 465 | // ui.PlatformDispatcher.onReportTimings. |
| 466 | bool frame_timings_report_scheduled_ = false; |
| 467 | |
| 468 | // Vector of FrameTiming::kCount * n timestamps for n frames whose timings |
| 469 | // have not been reported yet. Vector of ints instead of FrameTiming is stored |
| 470 | // here for easier conversions to Dart objects. |
| 471 | std::vector<int64_t> unreported_timings_; |
| 472 | |
| 473 | /// Manages the displays. This class is thread safe, can be accessed from any |
| 474 | /// of the threads. |
| 475 | std::unique_ptr<DisplayManager> display_manager_; |
| 476 | |
| 477 | // protects expected_frame_size_ which is set on platform thread and read on |
| 478 | // raster thread |
| 479 | std::mutex resize_mutex_; |
| 480 | |
| 481 | // used to discard wrong size layer tree produced during interactive resizing |
| 482 | SkISize expected_frame_size_ = SkISize::MakeEmpty(); |
| 483 | |
| 484 | // Used to communicate the right frame bounds via service protocol. |
| 485 | double device_pixel_ratio_ = 0.0; |
| 486 | |
| 487 | // How many frames have been timed since last report. |
| 488 | size_t UnreportedFramesCount() const; |
| 489 | |
| 490 | Shell(DartVMRef vm, |
| 491 | const TaskRunners& task_runners, |
| 492 | fml::RefPtr<fml::RasterThreadMerger> parent_merger, |
| 493 | const std::shared_ptr<ResourceCacheLimitCalculator>& |
| 494 | resource_cache_limit_calculator, |
| 495 | const Settings& settings, |
| 496 | std::shared_ptr<VolatilePathTracker> volatile_path_tracker, |
| 497 | bool is_gpu_disabled); |
| 498 | |
| 499 | static std::unique_ptr<Shell> CreateShellOnPlatformThread( |
| 500 | DartVMRef vm, |
| 501 | fml::RefPtr<fml::RasterThreadMerger> parent_merger, |
| 502 | std::shared_ptr<ShellIOManager> parent_io_manager, |
| 503 | const std::shared_ptr<ResourceCacheLimitCalculator>& |
| 504 | resource_cache_limit_calculator, |
| 505 | const TaskRunners& task_runners, |
| 506 | const PlatformData& platform_data, |
| 507 | const Settings& settings, |
| 508 | fml::RefPtr<const DartSnapshot> isolate_snapshot, |
| 509 | const Shell::CreateCallback<PlatformView>& on_create_platform_view, |
| 510 | const Shell::CreateCallback<Rasterizer>& on_create_rasterizer, |
| 511 | const EngineCreateCallback& on_create_engine, |
| 512 | bool is_gpu_disabled); |
| 513 | |
| 514 | static std::unique_ptr<Shell> CreateWithSnapshot( |
| 515 | const PlatformData& platform_data, |
| 516 | const TaskRunners& task_runners, |
| 517 | const fml::RefPtr<fml::RasterThreadMerger>& parent_thread_merger, |
| 518 | const std::shared_ptr<ShellIOManager>& parent_io_manager, |
| 519 | const std::shared_ptr<ResourceCacheLimitCalculator>& |
| 520 | resource_cache_limit_calculator, |
| 521 | Settings settings, |
| 522 | DartVMRef vm, |
| 523 | fml::RefPtr<const DartSnapshot> isolate_snapshot, |
| 524 | const CreateCallback<PlatformView>& on_create_platform_view, |
| 525 | const CreateCallback<Rasterizer>& on_create_rasterizer, |
| 526 | const EngineCreateCallback& on_create_engine, |
| 527 | bool is_gpu_disabled); |
| 528 | |
| 529 | bool Setup(std::unique_ptr<PlatformView> platform_view, |
| 530 | std::unique_ptr<Engine> engine, |
| 531 | std::unique_ptr<Rasterizer> rasterizer, |
| 532 | const std::shared_ptr<ShellIOManager>& io_manager); |
| 533 | |
| 534 | void ReportTimings(); |
| 535 | |
| 536 | // |PlatformView::Delegate| |
| 537 | void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override; |
| 538 | |
| 539 | // |PlatformView::Delegate| |
| 540 | void OnPlatformViewDestroyed() override; |
| 541 | |
| 542 | // |PlatformView::Delegate| |
| 543 | void OnPlatformViewScheduleFrame() override; |
| 544 | |
| 545 | // |PlatformView::Delegate| |
| 546 | void OnPlatformViewSetViewportMetrics( |
| 547 | int64_t view_id, |
| 548 | const ViewportMetrics& metrics) override; |
| 549 | |
| 550 | // |PlatformView::Delegate| |
| 551 | void OnPlatformViewDispatchPlatformMessage( |
| 552 | std::unique_ptr<PlatformMessage> message) override; |
| 553 | |
| 554 | // |PlatformView::Delegate| |
| 555 | void OnPlatformViewDispatchPointerDataPacket( |
| 556 | std::unique_ptr<PointerDataPacket> packet) override; |
| 557 | |
| 558 | // |PlatformView::Delegate| |
| 559 | void OnPlatformViewDispatchSemanticsAction(int32_t node_id, |
| 560 | SemanticsAction action, |
| 561 | fml::MallocMapping args) override; |
| 562 | |
| 563 | // |PlatformView::Delegate| |
| 564 | void OnPlatformViewSetSemanticsEnabled(bool enabled) override; |
| 565 | |
| 566 | // |shell:PlatformView::Delegate| |
| 567 | void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override; |
| 568 | |
| 569 | // |PlatformView::Delegate| |
| 570 | void OnPlatformViewRegisterTexture( |
| 571 | std::shared_ptr<flutter::Texture> texture) override; |
| 572 | |
| 573 | // |PlatformView::Delegate| |
| 574 | void OnPlatformViewUnregisterTexture(int64_t texture_id) override; |
| 575 | |
| 576 | // |PlatformView::Delegate| |
| 577 | void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override; |
| 578 | |
| 579 | // |PlatformView::Delegate| |
| 580 | void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override; |
| 581 | |
| 582 | // |PlatformView::Delegate| |
| 583 | const Settings& OnPlatformViewGetSettings() const override; |
| 584 | |
| 585 | // |PlatformView::Delegate| |
| 586 | void LoadDartDeferredLibrary( |
| 587 | intptr_t loading_unit_id, |
| 588 | std::unique_ptr<const fml::Mapping> snapshot_data, |
| 589 | std::unique_ptr<const fml::Mapping> snapshot_instructions) override; |
| 590 | |
| 591 | void LoadDartDeferredLibraryError(intptr_t loading_unit_id, |
| 592 | const std::string error_message, |
| 593 | bool transient) override; |
| 594 | |
| 595 | // |PlatformView::Delegate| |
| 596 | void UpdateAssetResolverByType( |
| 597 | std::unique_ptr<AssetResolver> updated_asset_resolver, |
| 598 | AssetResolver::AssetResolverType type) override; |
| 599 | |
| 600 | // |Animator::Delegate| |
| 601 | void OnAnimatorBeginFrame(fml::TimePoint frame_target_time, |
| 602 | uint64_t frame_number) override; |
| 603 | |
| 604 | // |Animator::Delegate| |
| 605 | void OnAnimatorNotifyIdle(fml::TimeDelta deadline) override; |
| 606 | |
| 607 | // |Animator::Delegate| |
| 608 | void OnAnimatorUpdateLatestFrameTargetTime( |
| 609 | fml::TimePoint frame_target_time) override; |
| 610 | |
| 611 | // |Animator::Delegate| |
| 612 | void OnAnimatorDraw(std::shared_ptr<LayerTreePipeline> pipeline) override; |
| 613 | |
| 614 | // |Animator::Delegate| |
| 615 | void OnAnimatorDrawLastLayerTree( |
| 616 | std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder) override; |
| 617 | |
| 618 | // |Engine::Delegate| |
| 619 | void OnEngineUpdateSemantics( |
| 620 | SemanticsNodeUpdates update, |
| 621 | CustomAccessibilityActionUpdates actions) override; |
| 622 | |
| 623 | // |Engine::Delegate| |
| 624 | void OnEngineHandlePlatformMessage( |
| 625 | std::unique_ptr<PlatformMessage> message) override; |
| 626 | |
| 627 | void HandleEngineSkiaMessage(std::unique_ptr<PlatformMessage> message); |
| 628 | |
| 629 | // |Engine::Delegate| |
| 630 | void OnPreEngineRestart() override; |
| 631 | |
| 632 | // |Engine::Delegate| |
| 633 | void OnRootIsolateCreated() override; |
| 634 | |
| 635 | // |Engine::Delegate| |
| 636 | void UpdateIsolateDescription(const std::string isolate_name, |
| 637 | int64_t isolate_port) override; |
| 638 | |
| 639 | // |Engine::Delegate| |
| 640 | void SetNeedsReportTimings(bool value) override; |
| 641 | |
| 642 | // |Engine::Delegate| |
| 643 | std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale( |
| 644 | const std::vector<std::string>& supported_locale_data) override; |
| 645 | |
| 646 | // |Engine::Delegate| |
| 647 | void RequestDartDeferredLibrary(intptr_t loading_unit_id) override; |
| 648 | |
| 649 | // |Engine::Delegate| |
| 650 | fml::TimePoint GetCurrentTimePoint() override; |
| 651 | |
| 652 | // |Rasterizer::Delegate| |
| 653 | void OnFrameRasterized(const FrameTiming&) override; |
| 654 | |
| 655 | // |Rasterizer::Delegate| |
| 656 | fml::Milliseconds GetFrameBudget() override; |
| 657 | |
| 658 | // |Rasterizer::Delegate| |
| 659 | fml::TimePoint GetLatestFrameTargetTime() const override; |
| 660 | |
| 661 | // |ServiceProtocol::Handler| |
| 662 | fml::RefPtr<fml::TaskRunner> GetServiceProtocolHandlerTaskRunner( |
| 663 | std::string_view method) const override; |
| 664 | |
| 665 | // |ServiceProtocol::Handler| |
| 666 | bool HandleServiceProtocolMessage( |
| 667 | std::string_view method, // one if the extension names specified above. |
| 668 | const ServiceProtocolMap& params, |
| 669 | rapidjson::Document* response) override; |
| 670 | |
| 671 | // |ServiceProtocol::Handler| |
| 672 | ServiceProtocol::Handler::Description GetServiceProtocolDescription() |
| 673 | const override; |
| 674 | |
| 675 | // Service protocol handler |
| 676 | bool OnServiceProtocolScreenshot( |
| 677 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 678 | rapidjson::Document* response); |
| 679 | |
| 680 | // Service protocol handler |
| 681 | bool OnServiceProtocolScreenshotSKP( |
| 682 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 683 | rapidjson::Document* response); |
| 684 | |
| 685 | // Service protocol handler |
| 686 | bool OnServiceProtocolRunInView( |
| 687 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 688 | rapidjson::Document* response); |
| 689 | |
| 690 | // Service protocol handler |
| 691 | bool OnServiceProtocolFlushUIThreadTasks( |
| 692 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 693 | rapidjson::Document* response); |
| 694 | |
| 695 | // Service protocol handler |
| 696 | bool OnServiceProtocolSetAssetBundlePath( |
| 697 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 698 | rapidjson::Document* response); |
| 699 | |
| 700 | // Service protocol handler |
| 701 | bool OnServiceProtocolGetDisplayRefreshRate( |
| 702 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 703 | rapidjson::Document* response); |
| 704 | |
| 705 | // Service protocol handler |
| 706 | // |
| 707 | // The returned SkSLs are base64 encoded. Decode before storing them to files. |
| 708 | bool OnServiceProtocolGetSkSLs( |
| 709 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 710 | rapidjson::Document* response); |
| 711 | |
| 712 | // Service protocol handler |
| 713 | bool OnServiceProtocolEstimateRasterCacheMemory( |
| 714 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 715 | rapidjson::Document* response); |
| 716 | |
| 717 | // Service protocol handler |
| 718 | // |
| 719 | // Renders a frame and responds with various statistics pertaining to the |
| 720 | // raster call. These include time taken to raster every leaf layer and also |
| 721 | // leaf layer snapshots. |
| 722 | bool OnServiceProtocolRenderFrameWithRasterStats( |
| 723 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 724 | rapidjson::Document* response); |
| 725 | |
| 726 | // Service protocol handler |
| 727 | // |
| 728 | // Forces the FontCollection to reload the font manifest. Used to support hot |
| 729 | // reload for fonts. |
| 730 | bool OnServiceProtocolReloadAssetFonts( |
| 731 | const ServiceProtocol::Handler::ServiceProtocolMap& params, |
| 732 | rapidjson::Document* response); |
| 733 | |
| 734 | // Send a system font change notification. |
| 735 | void SendFontChangeNotification(); |
| 736 | |
| 737 | // |ResourceCacheLimitItem| |
| 738 | size_t GetResourceCacheLimit() override { return resource_cache_limit_; }; |
| 739 | |
| 740 | // Creates an asset bundle from the original settings asset path or |
| 741 | // directory. |
| 742 | std::unique_ptr<DirectoryAssetBundle> RestoreOriginalAssetResolver(); |
| 743 | |
| 744 | // For accessing the Shell via the raster thread, necessary for various |
| 745 | // rasterizer callbacks. |
| 746 | std::unique_ptr<fml::TaskRunnerAffineWeakPtrFactory<Shell>> weak_factory_gpu_; |
| 747 | |
| 748 | fml::WeakPtrFactory<Shell> weak_factory_; |
| 749 | friend class testing::ShellTest; |
| 750 | |
| 751 | FML_DISALLOW_COPY_AND_ASSIGN(Shell); |
| 752 | }; |
| 753 | |
| 754 | } // namespace flutter |
| 755 | |
| 756 | #endif // SHELL_COMMON_SHELL_H_ |
| 757 | |