Skip to content

Commit 4c11ced

Browse files
authored
lightweight flutter engines: sharing skia contexts for the io thread (flutter#23508)
1 parent 3a89dff commit 4c11ced

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

shell/common/shell.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ std::unique_ptr<Shell> Shell::Spawn(
500500
}));
501501
RunConfiguration configuration =
502502
RunConfiguration::InferFromSettings(settings);
503+
result->shared_resource_context_ = io_manager_->GetSharedResourceContext();
503504
result->RunEngine(std::move(configuration));
504505
return result;
505506
}
@@ -762,10 +763,16 @@ void Shell::OnPlatformViewCreated(std::unique_ptr<Surface> surface) {
762763
FML_DCHECK(platform_view);
763764

764765
auto io_task = [io_manager = io_manager_->GetWeakPtr(), platform_view,
765-
ui_task_runner = task_runners_.GetUITaskRunner(), ui_task] {
766+
ui_task_runner = task_runners_.GetUITaskRunner(), ui_task,
767+
shared_resource_context = shared_resource_context_] {
766768
if (io_manager && !io_manager->GetResourceContext()) {
767-
io_manager->NotifyResourceContextAvailable(
768-
platform_view->CreateResourceContext());
769+
sk_sp<GrDirectContext> resource_context;
770+
if (shared_resource_context) {
771+
resource_context = shared_resource_context;
772+
} else {
773+
resource_context = platform_view->CreateResourceContext();
774+
}
775+
io_manager->NotifyResourceContextAvailable(resource_context);
769776
}
770777
// Step 1: Next, post a task on the UI thread to tell the engine that it has
771778
// an output surface.

shell/common/shell.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ class Shell final : public PlatformView::Delegate,
452452
// How many frames have been timed since last report.
453453
size_t UnreportedFramesCount() const;
454454

455+
sk_sp<GrDirectContext> shared_resource_context_;
456+
455457
Shell(DartVMRef vm,
456458
TaskRunners task_runners,
457459
Settings settings,

shell/common/shell_io_manager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class ShellIOManager final : public IOManager {
5555
// |IOManager|
5656
std::shared_ptr<fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() override;
5757

58+
sk_sp<GrDirectContext> GetSharedResourceContext() const {
59+
return resource_context_;
60+
};
61+
5862
private:
5963
// Resource context management.
6064
sk_sp<GrDirectContext> resource_context_;

shell/common/shell_unittests.cc

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ static void TestDartVmFlags(std::vector<const char*>& flags) {
218218
}
219219
}
220220

221+
static void PostSync(const fml::RefPtr<fml::TaskRunner>& task_runner,
222+
const fml::closure& task) {
223+
fml::AutoResetWaitableEvent latch;
224+
fml::TaskRunner::RunNowOrPostTask(task_runner, [&latch, &task] {
225+
task();
226+
latch.Signal();
227+
});
228+
latch.Wait();
229+
}
230+
221231
TEST_F(ShellTest, InitializeWithInvalidThreads) {
222232
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
223233
Settings settings = CreateSettingsForFixture();
@@ -2437,30 +2447,30 @@ TEST_F(ShellTest, Spawn) {
24372447
main_latch.Wait();
24382448
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
24392449

2440-
{
2441-
fml::AutoResetWaitableEvent latch;
2442-
fml::TaskRunner::RunNowOrPostTask(
2443-
shell->GetTaskRunners().GetPlatformTaskRunner(),
2444-
[this, &spawner = shell, &latch, settings]() {
2445-
MockPlatformViewDelegate platform_view_delegate;
2446-
auto spawn = spawner->Spawn(
2447-
settings,
2448-
[&platform_view_delegate](Shell& shell) {
2449-
auto result = std::make_unique<MockPlatformView>(
2450-
platform_view_delegate, shell.GetTaskRunners());
2451-
ON_CALL(*result, CreateRenderingSurface())
2452-
.WillByDefault(::testing::Invoke(
2453-
[] { return std::make_unique<MockSurface>(); }));
2454-
return result;
2455-
},
2456-
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
2457-
ASSERT_NE(nullptr, spawn.get());
2458-
ASSERT_TRUE(ValidateShell(spawn.get()));
2459-
DestroyShell(std::move(spawn));
2460-
latch.Signal();
2461-
});
2462-
latch.Wait();
2463-
}
2450+
PostSync(shell->GetTaskRunners().GetPlatformTaskRunner(), [this,
2451+
&spawner = shell,
2452+
settings]() {
2453+
MockPlatformViewDelegate platform_view_delegate;
2454+
auto spawn = spawner->Spawn(
2455+
settings,
2456+
[&platform_view_delegate](Shell& shell) {
2457+
auto result = std::make_unique<MockPlatformView>(
2458+
platform_view_delegate, shell.GetTaskRunners());
2459+
ON_CALL(*result, CreateRenderingSurface())
2460+
.WillByDefault(::testing::Invoke(
2461+
[] { return std::make_unique<MockSurface>(); }));
2462+
return result;
2463+
},
2464+
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
2465+
ASSERT_NE(nullptr, spawn.get());
2466+
ASSERT_TRUE(ValidateShell(spawn.get()));
2467+
2468+
PostSync(spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
2469+
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
2470+
spawn->GetIOManager()->GetResourceContext().get());
2471+
});
2472+
DestroyShell(std::move(spawn));
2473+
});
24642474

24652475
DestroyShell(std::move(shell));
24662476
ASSERT_FALSE(DartVMRef::IsInstanceRunning());

0 commit comments

Comments
 (0)