Skip to content

Commit 697012a

Browse files
authored
[impeller] parallelize opengles shader compiles (#194022)
> [!NOTE] > This PR depended on #193985 which refactored code to make way for this change. This PR was authored in lieu of #193066 and was informed by @krll-kov's great investigative work. design doc: https://github.com/flutter/rfc/blob/main/rfc/210.0001-parallel-gles-linking.md issue: #191860 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 96d0402 commit 697012a

6 files changed

Lines changed: 898 additions & 152 deletions

File tree

‎engine/src/flutter/impeller/renderer/backend/gles/pipeline_compile_queue_gles.cc‎

Lines changed: 141 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,105 @@
44

55
#include "impeller/renderer/backend/gles/pipeline_compile_queue_gles.h"
66

7+
#include <algorithm>
8+
79
#include "flutter/fml/logging.h"
810
#include "flutter/fml/trace_event.h"
911

1012
namespace impeller {
1113

1214
std::shared_ptr<PipelineCompileQueueGLES> PipelineCompileQueueGLES::Create(
13-
std::shared_ptr<fml::BasicTaskRunner> worker_task_runner) {
14-
if (!worker_task_runner) {
15+
std::shared_ptr<fml::BasicTaskRunner> worker_task_runner,
16+
size_t max_active_jobs) {
17+
if (!worker_task_runner || max_active_jobs == 0) {
1518
return nullptr;
1619
}
17-
return std::shared_ptr<PipelineCompileQueueGLES>(
18-
new PipelineCompileQueueGLES(std::move(worker_task_runner)));
20+
return std::shared_ptr<PipelineCompileQueueGLES>(new PipelineCompileQueueGLES(
21+
std::move(worker_task_runner), max_active_jobs));
1922
}
2023

2124
PipelineCompileQueueGLES::PipelineCompileQueueGLES(
22-
std::shared_ptr<fml::BasicTaskRunner> worker_task_runner)
23-
: worker_task_runner_(std::move(worker_task_runner)) {}
25+
std::shared_ptr<fml::BasicTaskRunner> worker_task_runner,
26+
size_t max_active_jobs)
27+
: worker_task_runner_(std::move(worker_task_runner)),
28+
max_active_jobs_(max_active_jobs) {}
2429

2530
PipelineCompileQueueGLES::~PipelineCompileQueueGLES() {
26-
// Flush any jobs still pending. Tasks already posted to the worker only hold
27-
// a weak reference to the queue and become no-ops.
28-
while (auto job = TakeNextJob()) {
29-
job();
31+
// Tasks already posted to the worker only hold a weak reference to the queue
32+
// and become no-ops.
33+
InactiveJobMap inactive_jobs;
34+
ActiveJobMap active_jobs;
35+
{
36+
Lock lock(mutex_);
37+
inactive_jobs.swap(inactive_jobs_);
38+
active_jobs.swap(active_jobs_);
39+
}
40+
// Flush any jobs that haven't been started.
41+
for (auto& [desc, job] : inactive_jobs) {
42+
PerformJobImmediately(*job);
43+
}
44+
// Jobs that have been started must be finished on the worker, the thread
45+
// that started them.
46+
for (auto& [desc, active_job] : active_jobs) {
47+
std::shared_ptr<CompileJob> job = std::move(active_job.job);
48+
worker_task_runner_->PostTask([job]() { job->Finish(); });
3049
}
3150
}
3251

3352
bool PipelineCompileQueueGLES::PostJobForDescriptor(
3453
const PipelineDescriptor& desc,
35-
const fml::closure& job) {
54+
std::unique_ptr<CompileJob> job) {
3655
if (!job) {
3756
return false;
3857
}
3958

40-
bool inserted = false;
59+
bool is_duplicate = false;
4160
bool should_schedule = false;
4261
{
4362
Lock lock(mutex_);
44-
inserted = pending_jobs_.insert({desc, job}).second;
45-
should_schedule = inserted && !is_processing_;
46-
if (should_schedule) {
63+
is_duplicate = inactive_jobs_.contains(desc) || active_jobs_.contains(desc);
64+
if (!is_duplicate) {
65+
inactive_jobs_.try_emplace(desc, std::move(job));
66+
should_schedule = !is_processing_;
4767
is_processing_ = true;
4868
}
4969
}
5070

51-
if (!inserted) {
71+
if (is_duplicate) {
5272
// This bit is being extremely conservative. If insertion did not take
5373
// place, someone gave the compile queue a job for the same descritor. This
5474
// is highly unusual but technically not impossible. Just run the job
5575
// eagerly.
5676
FML_LOG(WARNING) << "Got multiple compile jobs for the same descriptor. "
5777
"Running eagerly.";
58-
worker_task_runner_->PostTask(job);
78+
worker_task_runner_->PostTask(
79+
[job = std::shared_ptr<CompileJob>(std::move(job))]() {
80+
PerformJobImmediately(*job);
81+
});
5982
} else if (should_schedule) {
60-
ScheduleNextJob();
83+
ScheduleRun();
6184
}
6285
return true;
6386
}
6487

6588
void PipelineCompileQueueGLES::PerformJobEagerly(
6689
const PipelineDescriptor& desc) {
67-
fml::closure job;
90+
std::unique_ptr<CompileJob> job;
6891
{
6992
Lock lock(mutex_);
70-
auto found = pending_jobs_.find(desc);
71-
if (found == pending_jobs_.end()) {
93+
InactiveJobMap::iterator found = inactive_jobs_.find(desc);
94+
if (found == inactive_jobs_.end()) {
95+
ActiveJobMap::iterator active_job = active_jobs_.find(desc);
96+
if (active_job == active_jobs_.end()) {
97+
return;
98+
}
99+
// A job that has been started must be finished by the worker, the
100+
// thread that started it. Have the worker finish it next and wait for
101+
// that.
102+
active_job->second.awaited = true;
103+
active_job_removed_.Wait(mutex_, [&]() IPLR_REQUIRES(mutex_) {
104+
return !active_jobs_.contains(desc);
105+
});
72106
return;
73107
}
74108
// The pipeline compile job was somewhere in the task queue. However, a
@@ -83,34 +117,101 @@ void PipelineCompileQueueGLES::PerformJobEagerly(
83117
reinterpret_cast<int64_t>(this), // Trace Counter ID
84118
"PrioritiesElevated", priorities_elevated_);
85119
job = std::move(found->second);
86-
pending_jobs_.erase(found);
120+
inactive_jobs_.erase(found);
87121
}
88-
job();
122+
PerformJobImmediately(*job);
89123
}
90124

91-
void PipelineCompileQueueGLES::ScheduleNextJob() {
125+
void PipelineCompileQueueGLES::ScheduleRun() {
92126
worker_task_runner_->PostTask([weak_queue = weak_from_this()]() {
93-
auto queue = weak_queue.lock();
94-
if (!queue) {
95-
return;
96-
}
97-
if (auto job = queue->TakeNextJob()) {
98-
job();
99-
queue->ScheduleNextJob();
127+
if (std::shared_ptr<PipelineCompileQueueGLES> queue = weak_queue.lock()) {
128+
queue->Run();
100129
}
101130
});
102131
}
103132

104-
fml::closure PipelineCompileQueueGLES::TakeNextJob() {
105-
Lock lock(mutex_);
106-
if (pending_jobs_.empty()) {
107-
is_processing_ = false;
108-
return nullptr;
133+
void PipelineCompileQueueGLES::Run() {
134+
ActiveJobMap::iterator active_job;
135+
bool should_start = false;
136+
{
137+
Lock lock(mutex_);
138+
if (ActiveJobMap::iterator awaited_job = FindAwaitedActiveJob();
139+
awaited_job != active_jobs_.end()) {
140+
// A thread is blocked waiting on this job, so finish it first.
141+
active_job = awaited_job;
142+
} else if (active_jobs_.size() < max_active_jobs_ &&
143+
!inactive_jobs_.empty()) {
144+
// There is capacity for another active job, so start the oldest one.
145+
active_job = ActivateOldestInactiveJob();
146+
should_start = true;
147+
} else if (!active_jobs_.empty()) {
148+
// At capacity or nothing left to start, so finish an active job.
149+
active_job = active_jobs_.begin();
150+
} else {
151+
// No work remains; stop processing until a new job is posted.
152+
is_processing_ = false;
153+
return;
154+
}
155+
}
156+
// Only this method adds or removes active jobs, so the iterator remains
157+
// valid while the job is started or finished outside of the lock.
158+
if (should_start) {
159+
StartActiveJob(active_job);
160+
} else {
161+
FinishActiveJob(active_job);
162+
}
163+
ScheduleRun();
164+
}
165+
166+
PipelineCompileQueueGLES::ActiveJobMap::iterator
167+
PipelineCompileQueueGLES::FindAwaitedActiveJob() {
168+
return std::find_if(active_jobs_.begin(), active_jobs_.end(),
169+
[](const ActiveJobMap::value_type& entry) {
170+
return entry.second.awaited;
171+
});
172+
}
173+
174+
PipelineCompileQueueGLES::ActiveJobMap::iterator
175+
PipelineCompileQueueGLES::ActivateOldestInactiveJob() {
176+
InactiveJobMap::iterator inactive_job = inactive_jobs_.begin();
177+
ActiveJobMap::iterator active_job =
178+
active_jobs_
179+
.try_emplace(inactive_job->first,
180+
ActiveJob{.job = std::move(inactive_job->second)})
181+
.first;
182+
inactive_jobs_.erase(inactive_job);
183+
return active_job;
184+
}
185+
186+
void PipelineCompileQueueGLES::StartActiveJob(
187+
ActiveJobMap::iterator active_job) {
188+
if (!active_job->second.job->Start().ok()) {
189+
RemoveActiveJob(active_job);
190+
}
191+
}
192+
193+
void PipelineCompileQueueGLES::FinishActiveJob(
194+
ActiveJobMap::iterator active_job) {
195+
active_job->second.job->Finish();
196+
RemoveActiveJob(active_job);
197+
}
198+
199+
void PipelineCompileQueueGLES::RemoveActiveJob(
200+
ActiveJobMap::iterator active_job) {
201+
std::unique_ptr<CompileJob> job;
202+
{
203+
Lock lock(mutex_);
204+
job = std::move(active_job->second.job);
205+
active_jobs_.erase(active_job);
206+
}
207+
active_job_removed_.NotifyAll();
208+
}
209+
210+
void PipelineCompileQueueGLES::PerformJobImmediately(CompileJob& job) {
211+
if (!job.Start().ok()) {
212+
return;
109213
}
110-
auto job_iterator = pending_jobs_.begin();
111-
auto job = std::move(job_iterator->second);
112-
pending_jobs_.erase(job_iterator);
113-
return job;
214+
job.Finish();
114215
}
115216

116217
} // namespace impeller

0 commit comments

Comments
 (0)