Skip to content

Commit

Permalink
Ensure the ResourceContext is not ripped out from under dart (flutter…
Browse files Browse the repository at this point in the history
…#7528)

* Ensure the ResourceContext is not ripped out from under dart
  • Loading branch information
dnfield authored Jan 17, 2019
1 parent 270e9a7 commit 4acfced
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions shell/common/io_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ fml::WeakPtr<GrContext> IOManager::GetResourceContext() const {
: fml::WeakPtr<GrContext>();
}

void IOManager::NotifyResourceContextAvailable(
sk_sp<GrContext> 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<GrContext> resource_context) {
resource_context_ = std::move(resource_context);
resource_context_weak_factory_ =
Expand Down
9 changes: 9 additions & 0 deletions shell/common/io_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class IOManager : public blink::IOManager {

fml::WeakPtr<GrContext> 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<GrContext> 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<GrContext> resource_context);

fml::RefPtr<flow::SkiaUnrefQueue> GetSkiaUnrefQueue() const override;
Expand Down
13 changes: 9 additions & 4 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,22 @@ void Shell::OnPlatformViewCreated(std::unique_ptr<Surface> 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
// an output 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();
}

Expand Down

0 comments on commit 4acfced

Please sign in to comment.