From e53df3e546c5903b8bca483f5107594c7a6da4aa Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 17 Jan 2019 13:55:44 -0800 Subject: [PATCH] Ensure the ResourceContext is not ripped out from under dart (flutter/engine#7528) * Ensure the ResourceContext is not ripped out from under dart --- engine/src/flutter/shell/common/io_manager.cc | 10 ++++++++++ engine/src/flutter/shell/common/io_manager.h | 9 +++++++++ engine/src/flutter/shell/common/shell.cc | 13 +++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/shell/common/io_manager.cc b/engine/src/flutter/shell/common/io_manager.cc index 84bbbdc5c7..b5393831cd 100644 --- a/engine/src/flutter/shell/common/io_manager.cc +++ b/engine/src/flutter/shell/common/io_manager.cc @@ -71,6 +71,16 @@ fml::WeakPtr IOManager::GetResourceContext() const { : fml::WeakPtr(); } +void IOManager::NotifyResourceContextAvailable( + sk_sp resource_context) { + // The resource context needs to survive as long as we have Dart objects + // referencing. We shouldn't ever need to replace it if we have one - unless + // we've somehow shut down the Dart VM and started a new one fresh. + if (!resource_context_) { + UpdateResourceContext(std::move(resource_context)); + } +} + void IOManager::UpdateResourceContext(sk_sp resource_context) { resource_context_ = std::move(resource_context); resource_context_weak_factory_ = diff --git a/engine/src/flutter/shell/common/io_manager.h b/engine/src/flutter/shell/common/io_manager.h index bb20781bf0..11934f999a 100644 --- a/engine/src/flutter/shell/common/io_manager.h +++ b/engine/src/flutter/shell/common/io_manager.h @@ -30,6 +30,15 @@ class IOManager : public blink::IOManager { fml::WeakPtr GetResourceContext() const override; + // This method should be called when a resource_context first becomes + // available. It is safe to call multiple times, and will only update + // the held resource context if it has not already been set. + void NotifyResourceContextAvailable(sk_sp resource_context); + + // This method should be called if you want to force the IOManager to + // update its resource context reference. It should not be called + // if there are any Dart objects that have a reference to the old + // resource context, but may be called if the Dart VM is restarted. void UpdateResourceContext(sk_sp resource_context); fml::RefPtr GetSkiaUnrefQueue() const override; diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 063533462b..c517946f2c 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -446,7 +446,7 @@ void Shell::OnPlatformViewCreated(std::unique_ptr surface) { platform_view = platform_view_->GetWeakPtr(), ui_task_runner = task_runners_.GetUITaskRunner(), ui_task] { if (io_manager) { - io_manager->UpdateResourceContext( + io_manager->NotifyResourceContextAvailable( platform_view ? platform_view->CreateResourceContext() : nullptr); } // Step 1: Next, post a task on the UI thread to tell the engine that it has @@ -454,9 +454,14 @@ void Shell::OnPlatformViewCreated(std::unique_ptr surface) { fml::TaskRunner::RunNowOrPostTask(ui_task_runner, ui_task); }; - // Step 0: Tell the IO thread that the PlatformView has a GLContext that can - // be used to create a resource context. - fml::TaskRunner::RunNowOrPostTask(task_runners_.GetIOTaskRunner(), io_task); + // Step 0: If the IOManager doesn't already have a ResourceContext, tell the + // IO thread that the PlatformView can make one for it now. + // Otherwise, jump right to step 1 on the UI thread. + if (!io_manager_->GetResourceContext()) { + fml::TaskRunner::RunNowOrPostTask(task_runners_.GetIOTaskRunner(), io_task); + } else { + fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(), ui_task); + } latch.Wait(); }