Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
pipeline_compile_queue.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
9
10namespace impeller {
11
15
17 const fml::closure& job) {
18 if (!job) {
19 return false;
20 }
21
22 if (!AddJob(desc, job)) {
23 // This bit is being extremely conservative. If insertion did not take
24 // place, someone gave the compile queue a job for the same description.
25 // This is highly unusual but technically not impossible. Just run the job
26 // eagerly.
27 FML_LOG(WARNING) << "Got multiple compile jobs for the same descriptor. "
28 "Running eagerly.";
29 PostJob(job);
30 return true;
31 }
32
33 OnJobAdded();
34 return true;
35}
36
38 const fml::closure& job) {
39 Lock lock(pending_jobs_mutex_);
40 auto insertion_result = pending_jobs_.insert(std::make_pair(desc, job));
41 return insertion_result.second;
42}
43
45 Lock lock(pending_jobs_mutex_);
46 return !pending_jobs_.empty();
47}
48
49fml::closure PipelineCompileQueue::TakeNextJob() {
50 Lock lock(pending_jobs_mutex_);
51 if (pending_jobs_.empty()) {
52 return nullptr;
53 }
54 auto job_iterator = pending_jobs_.begin();
55 auto job = job_iterator->second;
56 pending_jobs_.erase(job_iterator);
57 return job;
58}
59
60fml::closure PipelineCompileQueue::TakeJob(const PipelineDescriptor& desc) {
61 Lock lock(pending_jobs_mutex_);
62 auto found = pending_jobs_.find(desc);
63 if (found == pending_jobs_.end()) {
64 return nullptr;
65 }
66 // The pipeline compile job was somewhere in the task queue. However, a
67 // rendering operation needed the job to be done ASAP. Instead of waiting for
68 // the pipeline compile queue to eventually get to finishing job, the thread
69 // waiting on the job just decided to take the job from the queue and do it
70 // itself. If there were jobs ahead of this one, it means that they were
71 // mis-prioritized. This counter dumps the number of job re-prioritizations.
72 priorities_elevated_++;
73 FML_TRACE_COUNTER("impeller", "PipelineCompileQueue",
74 reinterpret_cast<int64_t>(this), // Trace Counter ID
75 "PrioritiesElevated", priorities_elevated_);
76 auto job = found->second;
77 pending_jobs_.erase(found);
78 return job;
79}
80
82 if (auto job = TakeNextJob()) {
83 job();
84 }
85}
86
87void PipelineCompileQueue::FinishAllJobs() {
88 // This doesn't have to be fast. Just ensures the task queue is flushed when
89 // the compile queue is shutting down with jobs still in it.
90 while (true) {
91 bool has_jobs = false;
92 {
93 Lock lock(pending_jobs_mutex_);
94 has_jobs = !pending_jobs_.empty();
95 }
96 if (!has_jobs) {
97 return;
98 }
99 // Allow any remaining worker threads to take jobs from this queue.
100 DoOneJob();
101 }
102}
103
105 if (auto job = TakeJob(desc)) {
106 job();
107 }
108}
109
110} // namespace impeller
virtual void PostJob(const fml::closure &job)=0
Post a compilation job to the worker task runner.
bool PostJobForDescriptor(const PipelineDescriptor &desc, const fml::closure &job)
Post a compile job for the specified descriptor.
bool AddJob(const PipelineDescriptor &desc, const fml::closure &job)
Add a compilation job to the pending queue for the specified descriptor.
void DoOneJob()
Execute one pending compilation job from the queue.
bool HasPendingJobs()
Check if there are any pending compilation jobs in the queue.
virtual void OnJobAdded()=0
Called by PostJobForDescriptor after a job has been successfully added to the queue....
void PerformJobEagerly(const PipelineDescriptor &desc)
If the task has not yet been done, perform it eagerly on the calling thread. This can be used in lieu...
#define FML_LOG(severity)
Definition logging.h:101
std::function< void()> closure
Definition closure.h:14
#define FML_TRACE_COUNTER(category_group, name, counter_id, arg1,...)
Definition trace_event.h:85