lightweight flutter engines: sharing skia contexts for the io thread (flutter/engine#23508)

This commit is contained in:
gaaclarke
2021-01-13 13:04:16 -08:00
committed by GitHub
parent 048c639f18
commit 5026c62732
4 changed files with 50 additions and 27 deletions

View File

@@ -500,6 +500,7 @@ std::unique_ptr<Shell> Shell::Spawn(
}));
RunConfiguration configuration =
RunConfiguration::InferFromSettings(settings);
result->shared_resource_context_ = io_manager_->GetSharedResourceContext();
result->RunEngine(std::move(configuration));
return result;
}
@@ -762,10 +763,16 @@ void Shell::OnPlatformViewCreated(std::unique_ptr<Surface> surface) {
FML_DCHECK(platform_view);
auto io_task = [io_manager = io_manager_->GetWeakPtr(), platform_view,
ui_task_runner = task_runners_.GetUITaskRunner(), ui_task] {
ui_task_runner = task_runners_.GetUITaskRunner(), ui_task,
shared_resource_context = shared_resource_context_] {
if (io_manager && !io_manager->GetResourceContext()) {
io_manager->NotifyResourceContextAvailable(
platform_view->CreateResourceContext());
sk_sp<GrDirectContext> resource_context;
if (shared_resource_context) {
resource_context = shared_resource_context;
} else {
resource_context = platform_view->CreateResourceContext();
}
io_manager->NotifyResourceContextAvailable(resource_context);
}
// Step 1: Next, post a task on the UI thread to tell the engine that it has
// an output surface.

View File

@@ -452,6 +452,8 @@ class Shell final : public PlatformView::Delegate,
// How many frames have been timed since last report.
size_t UnreportedFramesCount() const;
sk_sp<GrDirectContext> shared_resource_context_;
Shell(DartVMRef vm,
TaskRunners task_runners,
Settings settings,

View File

@@ -55,6 +55,10 @@ class ShellIOManager final : public IOManager {
// |IOManager|
std::shared_ptr<fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() override;
sk_sp<GrDirectContext> GetSharedResourceContext() const {
return resource_context_;
};
private:
// Resource context management.
sk_sp<GrDirectContext> resource_context_;

View File

@@ -218,6 +218,16 @@ static void TestDartVmFlags(std::vector<const char*>& flags) {
}
}
static void PostSync(const fml::RefPtr<fml::TaskRunner>& task_runner,
const fml::closure& task) {
fml::AutoResetWaitableEvent latch;
fml::TaskRunner::RunNowOrPostTask(task_runner, [&latch, &task] {
task();
latch.Signal();
});
latch.Wait();
}
TEST_F(ShellTest, InitializeWithInvalidThreads) {
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
Settings settings = CreateSettingsForFixture();
@@ -2437,30 +2447,30 @@ TEST_F(ShellTest, Spawn) {
main_latch.Wait();
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
{
fml::AutoResetWaitableEvent latch;
fml::TaskRunner::RunNowOrPostTask(
shell->GetTaskRunners().GetPlatformTaskRunner(),
[this, &spawner = shell, &latch, settings]() {
MockPlatformViewDelegate platform_view_delegate;
auto spawn = spawner->Spawn(
settings,
[&platform_view_delegate](Shell& shell) {
auto result = std::make_unique<MockPlatformView>(
platform_view_delegate, shell.GetTaskRunners());
ON_CALL(*result, CreateRenderingSurface())
.WillByDefault(::testing::Invoke(
[] { return std::make_unique<MockSurface>(); }));
return result;
},
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
ASSERT_NE(nullptr, spawn.get());
ASSERT_TRUE(ValidateShell(spawn.get()));
DestroyShell(std::move(spawn));
latch.Signal();
});
latch.Wait();
}
PostSync(shell->GetTaskRunners().GetPlatformTaskRunner(), [this,
&spawner = shell,
settings]() {
MockPlatformViewDelegate platform_view_delegate;
auto spawn = spawner->Spawn(
settings,
[&platform_view_delegate](Shell& shell) {
auto result = std::make_unique<MockPlatformView>(
platform_view_delegate, shell.GetTaskRunners());
ON_CALL(*result, CreateRenderingSurface())
.WillByDefault(::testing::Invoke(
[] { return std::make_unique<MockSurface>(); }));
return result;
},
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
ASSERT_NE(nullptr, spawn.get());
ASSERT_TRUE(ValidateShell(spawn.get()));
PostSync(spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
spawn->GetIOManager()->GetResourceContext().get());
});
DestroyShell(std::move(spawn));
});
DestroyShell(std::move(shell));
ASSERT_FALSE(DartVMRef::IsInstanceRunning());