Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit de489e3

Browse files
author
Jonah Williams
authored
[Impeller] Give Impeller a dedicated raster priority level worker loop. (#43166)
We'd like to (or already are) using the concurrent message loop for high priority rendering tasks like PSO construction and render pass encoding. The default priority level for the engine managed concurrent message loop is 2, which is a significantly lower priority than the raster thread at -5. This is almost certainly causing priority inversion. We must move back to dedicated runners so we can adjust thread priorities.
1 parent 77a27b4 commit de489e3

39 files changed

+156
-185
lines changed

ci/licenses_golden/excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@
230230
../../../flutter/shell/platform/android/.gitignore
231231
../../../flutter/shell/platform/android/android_context_gl_impeller_unittests.cc
232232
../../../flutter/shell/platform/android/android_context_gl_unittests.cc
233-
../../../flutter/shell/platform/android/android_context_vulkan_impeller_unittests.cc
234233
../../../flutter/shell/platform/android/android_shell_holder_unittests.cc
235234
../../../flutter/shell/platform/android/apk_asset_provider_unittests.cc
236235
../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc

fml/concurrent_message_loop.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ConcurrentMessageLoop::ConcurrentMessageLoop(size_t worker_count)
2929
ConcurrentMessageLoop::~ConcurrentMessageLoop() {
3030
Terminate();
3131
for (auto& worker : workers_) {
32+
FML_DCHECK(worker.joinable());
3233
worker.join();
3334
}
3435
}

impeller/playground/backend/metal/playground_impl_mtl.mm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@
7373
if (!window) {
7474
return;
7575
}
76-
auto worker_task_runner = concurrent_loop_->GetTaskRunner();
77-
auto context = ContextMTL::Create(
78-
ShaderLibraryMappingsForPlayground(), worker_task_runner,
79-
is_gpu_disabled_sync_switch_, "Playground Library");
76+
auto context =
77+
ContextMTL::Create(ShaderLibraryMappingsForPlayground(),
78+
is_gpu_disabled_sync_switch_, "Playground Library");
8079
if (!context) {
8180
return;
8281
}

impeller/playground/backend/vulkan/playground_impl_vk.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ void PlaygroundImplVK::DestroyWindowHandle(WindowHandle handle) {
5252
}
5353

5454
PlaygroundImplVK::PlaygroundImplVK(PlaygroundSwitches switches)
55-
: PlaygroundImpl(switches),
56-
concurrent_loop_(fml::ConcurrentMessageLoop::Create()),
57-
handle_(nullptr, &DestroyWindowHandle) {
55+
: PlaygroundImpl(switches), handle_(nullptr, &DestroyWindowHandle) {
5856
if (!::glfwVulkanSupported()) {
5957
#ifdef TARGET_OS_MAC
6058
VALIDATION_LOG << "Attempted to initialize a Vulkan playground on macOS "
@@ -86,7 +84,6 @@ PlaygroundImplVK::PlaygroundImplVK(PlaygroundSwitches switches)
8684
&::glfwGetInstanceProcAddress);
8785
context_settings.shader_libraries_data = ShaderLibraryMappingsForPlayground();
8886
context_settings.cache_directory = fml::paths::GetCachesDirectory();
89-
context_settings.worker_task_runner = concurrent_loop_->GetTaskRunner();
9087
context_settings.enable_validation = switches_.enable_vulkan_validation;
9188

9289
auto context = ContextVK::Create(std::move(context_settings));

impeller/playground/backend/vulkan/playground_impl_vk.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#pragma once
66

7-
#include "flutter/fml/concurrent_message_loop.h"
87
#include "flutter/fml/macros.h"
98
#include "impeller/playground/playground_impl.h"
109
#include "impeller/renderer/backend/vulkan/vk.h"
@@ -18,7 +17,6 @@ class PlaygroundImplVK final : public PlaygroundImpl {
1817
~PlaygroundImplVK();
1918

2019
private:
21-
std::shared_ptr<fml::ConcurrentMessageLoop> concurrent_loop_;
2220
std::shared_ptr<Context> context_;
2321

2422
// Windows management.

impeller/playground/playground.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ void Playground::SetupWindow() {
139139
}
140140

141141
void Playground::TeardownWindow() {
142+
if (context_) {
143+
context_->Shutdown();
144+
}
142145
context_.reset();
143146
renderer_.reset();
144147
impl_.reset();

impeller/renderer/backend/gles/context_gles.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ bool ContextGLES::IsValid() const {
107107
return is_valid_;
108108
}
109109

110+
void ContextGLES::Shutdown() {}
111+
110112
// |Context|
111113
std::string ContextGLES::DescribeGpuModel() const {
112114
return reactor_->GetProcTable().GetDescription()->GetString();

impeller/renderer/backend/gles/context_gles.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class ContextGLES final : public Context,
7272
// |Context|
7373
const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
7474

75+
// |Context|
76+
void Shutdown() override;
77+
7578
FML_DISALLOW_COPY_AND_ASSIGN(ContextGLES);
7679
};
7780

impeller/renderer/backend/metal/command_buffer_mtl.mm

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,32 +202,39 @@ static bool LogMTLCommandBufferErrorIfPresent(id<MTLCommandBuffer> buffer) {
202202
return false;
203203
}
204204

205-
auto task = fml::MakeCopyable([render_pass, buffer, render_command_encoder,
206-
weak_context = context_]() {
207-
auto context = weak_context.lock();
208-
if (!context) {
209-
return;
210-
}
211-
auto is_gpu_disabled_sync_switch =
212-
ContextMTL::Cast(*context).GetIsGpuDisabledSyncSwitch();
213-
is_gpu_disabled_sync_switch->Execute(fml::SyncSwitch::Handlers().SetIfFalse(
214-
[&render_pass, &render_command_encoder, &buffer, &context] {
215-
auto mtl_render_pass = static_cast<RenderPassMTL*>(render_pass.get());
216-
if (!mtl_render_pass->label_.empty()) {
217-
[render_command_encoder
218-
setLabel:@(mtl_render_pass->label_.c_str())];
219-
}
220-
221-
auto result = mtl_render_pass->EncodeCommands(
222-
context->GetResourceAllocator(), render_command_encoder);
205+
auto task = fml::MakeCopyable(
206+
[render_pass, buffer, render_command_encoder, weak_context = context_]() {
207+
auto context = weak_context.lock();
208+
if (!context) {
223209
[render_command_encoder endEncoding];
224-
if (result) {
225-
[buffer commit];
226-
} else {
227-
VALIDATION_LOG << "Failed to encode command buffer";
228-
}
229-
}));
230-
});
210+
return;
211+
}
212+
auto is_gpu_disabled_sync_switch =
213+
ContextMTL::Cast(*context).GetIsGpuDisabledSyncSwitch();
214+
is_gpu_disabled_sync_switch->Execute(
215+
fml::SyncSwitch::Handlers()
216+
.SetIfFalse([&render_pass, &render_command_encoder, &buffer,
217+
&context] {
218+
auto mtl_render_pass =
219+
static_cast<RenderPassMTL*>(render_pass.get());
220+
if (!mtl_render_pass->label_.empty()) {
221+
[render_command_encoder
222+
setLabel:@(mtl_render_pass->label_.c_str())];
223+
}
224+
225+
auto result = mtl_render_pass->EncodeCommands(
226+
context->GetResourceAllocator(), render_command_encoder);
227+
[render_command_encoder endEncoding];
228+
if (result) {
229+
[buffer commit];
230+
} else {
231+
VALIDATION_LOG << "Failed to encode command buffer";
232+
}
233+
})
234+
.SetIfTrue([&render_command_encoder] {
235+
[render_command_encoder endEncoding];
236+
}));
237+
});
231238
worker_task_runner->PostTask(task);
232239
return true;
233240
}

impeller/renderer/backend/metal/context_mtl.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,17 @@ class ContextMTL final : public Context,
3535
public:
3636
static std::shared_ptr<ContextMTL> Create(
3737
const std::vector<std::string>& shader_library_paths,
38-
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
3938
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
4039

4140
static std::shared_ptr<ContextMTL> Create(
4241
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
43-
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
4442
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
4543
const std::string& label);
4644

4745
static std::shared_ptr<ContextMTL> Create(
4846
id<MTLDevice> device,
4947
id<MTLCommandQueue> command_queue,
5048
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
51-
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
5249
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
5350
const std::string& label);
5451

@@ -84,9 +81,12 @@ class ContextMTL final : public Context,
8481
// |Context|
8582
bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override;
8683

84+
// |Context|
85+
void Shutdown() override;
86+
8787
id<MTLCommandBuffer> CreateMTLCommandBuffer(const std::string& label) const;
8888

89-
const std::shared_ptr<fml::ConcurrentTaskRunner>& GetWorkerTaskRunner() const;
89+
const std::shared_ptr<fml::ConcurrentTaskRunner> GetWorkerTaskRunner() const;
9090

9191
std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() const;
9292

@@ -98,15 +98,14 @@ class ContextMTL final : public Context,
9898
std::shared_ptr<SamplerLibrary> sampler_library_;
9999
std::shared_ptr<AllocatorMTL> resource_allocator_;
100100
std::shared_ptr<const Capabilities> device_capabilities_;
101-
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner_;
101+
std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
102102
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
103103
bool is_valid_ = false;
104104

105105
ContextMTL(
106106
id<MTLDevice> device,
107107
id<MTLCommandQueue> command_queue,
108108
NSArray<id<MTLLibrary>>* shader_libraries,
109-
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
110109
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
111110

112111
std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(

0 commit comments

Comments
 (0)