| Flutter Impeller |

fence_waiter_vk.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
5 #include "impeller/renderer/backend/vulkan/fence_waiter_vk.h"
6
7 #include <algorithm>
8 #include <chrono>
9 #include <utility>
10
11 #include "flutter/fml/cpu_affinity.h"
12 #include "flutter/fml/thread.h"
13 #include "flutter/fml/trace_event.h"
14 #include "impeller/base/validation.h"
15
16 namespace impeller {
17
18 class WaitSetEntry {
19 public:
20 static std::shared_ptr<WaitSetEntry> Create(vk::UniqueFence p_fence,
21 const fml::closure& p_callback) {
22 return std::shared_ptr<WaitSetEntry>(
23 new WaitSetEntry(std::move(p_fence), p_callback));
24 }
25
26 void UpdateSignalledStatus(const vk::Device& device) {
27 if (is_signalled_) {
28 return;
29 }
30 is_signalled_ = device.getFenceStatus(fence_.get()) == vk::Result::eSuccess;
31 }
32
33 const vk::Fence& GetFence() const { return fence_.get(); }
34
35 bool IsSignalled() const { return is_signalled_; }
36
37 private:
38 vk::UniqueFence fence_;
39 fml::ScopedCleanupClosure callback_;
40 bool is_signalled_ = false;
41
42 WaitSetEntry(vk::UniqueFence p_fence, const fml::closure& p_callback)
43 : fence_(std::move(p_fence)),
44 callback_(fml::ScopedCleanupClosure{p_callback}) {}
45
46 FML_DISALLOW_COPY_ASSIGN_AND_MOVE(WaitSetEntry);
47 };
48
49 FenceWaiterVK::FenceWaiterVK(std::weak_ptr<DeviceHolder> device_holder)
50 : device_holder_(std::move(device_holder)) {
51 waiter_thread_ = std::make_unique<std::thread>([&]() { Main(); });
52 }
53
54 FenceWaiterVK::~FenceWaiterVK() {
55 Terminate();
56 waiter_thread_->join();
57 }
58
59 bool FenceWaiterVK::AddFence(vk::UniqueFence fence,
60 const fml::closure& callback) {
61 TRACE_EVENT0("flutter", "FenceWaiterVK::AddFence");
62 if (!fence || !callback) {
63 return false;
64 }
65 {
66 // Maintain the invariant that terminate_ is accessed only under the lock.
67 std::scoped_lock lock(wait_set_mutex_);
68 if (terminate_) {
69 return false;
70 }
71 wait_set_.emplace_back(WaitSetEntry::Create(std::move(fence), callback));
72 }
73 wait_set_cv_.notify_one();
74 return true;
75 }
76
77 static std::vector<vk::Fence> GetFencesForWaitSet(const WaitSet& set) {
78 std::vector<vk::Fence> fences;
79 for (const auto& entry : set) {
80 if (!entry->IsSignalled()) {
81 fences.emplace_back(entry->GetFence());
82 }
83 }
84 return fences;
85 }
86
87 void FenceWaiterVK::Main() {
88 fml::Thread::SetCurrentThreadName(
89 fml::Thread::ThreadConfig{"io.flutter.impeller.fence_waiter"});
90 // Since this thread mostly waits on fences, it doesn't need to be fast.
91 fml::RequestAffinity(fml::CpuAffinity::kEfficiency);
92
93 while (true) {
94 // We'll read the terminate_ flag within the lock below.
95 bool terminate = false;
96
97 {
98 std::unique_lock lock(wait_set_mutex_);
99
100 // If there are no fences to wait on, wait on the condition variable.
101 wait_set_cv_.wait(lock,
102 [&]() { return !wait_set_.empty() || terminate_; });
103
104 // Still under the lock, check if the waiter has been terminated.
105 terminate = terminate_;
106 }
107
108 if (terminate) {
109 WaitUntilEmpty();
110 break;
111 }
112
113 if (!Wait()) {
114 break;
115 }
116 }
117 }
118
119 void FenceWaiterVK::WaitUntilEmpty() {
120 // Note, there is no lock because once terminate_ is set to true, no other
121 // fence can be added to the wait set. Just in case, here's a FML_DCHECK:
122 FML_DCHECK(terminate_) << "Fence waiter must be terminated.";
123 while (!wait_set_.empty() && Wait()) {
124 // Intentionally empty.
125 }
126 }
127
128 bool FenceWaiterVK::Wait() {
129 // Snapshot the wait set and wait on the fences.
130 WaitSet wait_set;
131 {
132 std::scoped_lock lock(wait_set_mutex_);
133 wait_set = wait_set_;
134 }
135
136 using namespace std::literals::chrono_literals;
137
138 // Check if the context had died in the meantime.
139 auto device_holder = device_holder_.lock();
140 if (!device_holder) {
141 return false;
142 }
143
144 const auto& device = device_holder->GetDevice();
145 // Wait for one or more fences to be signaled. Any additional fences added
146 // to the waiter will be serviced in the next pass. If a fence that is going
147 // to be signaled at an abnormally long deadline is the only one in the set,
148 // a timeout will bail out the wait.
149 auto fences = GetFencesForWaitSet(wait_set);
150 if (fences.empty()) {
151 return true;
152 }
153
154 auto result = device.waitForFences(
155 /*fenceCount=*/fences.size(),
156 /*pFences=*/fences.data(),
157 /*waitAll=*/false,
158 /*timeout=*/std::chrono::nanoseconds{100ms}.count());
159 if (!(result == vk::Result::eSuccess || result == vk::Result::eTimeout)) {
160 VALIDATION_LOG << "Fence waiter encountered an unexpected error. Tearing "
161 "down the waiter thread.";
162 return false;
163 }
164
165 // One or more fences have been signaled. Find out which ones and update
166 // their signaled statuses.
167 {
168 TRACE_EVENT0("impeller", "CheckFenceStatus");
169 for (auto& entry : wait_set) {
170 entry->UpdateSignalledStatus(device);
171 }
172 wait_set.clear();
173 }
174
175 // Quickly acquire the wait set lock and erase signaled entries. Make sure
176 // the mutex is unlocked before calling the destructors of the erased
177 // entries. These might touch allocators.
178 WaitSet erased_entries;
179 {
180 static constexpr auto is_signalled = [](const auto& entry) {
181 return entry->IsSignalled();
182 };
183 std::scoped_lock lock(wait_set_mutex_);
184
185 // TODO(matanlurey): Iterate the list 1x by copying is_signaled into erased.
186 std::copy_if(wait_set_.begin(), wait_set_.end(),
187 std::back_inserter(erased_entries), is_signalled);
188 wait_set_.erase(
189 std::remove_if(wait_set_.begin(), wait_set_.end(), is_signalled),
190 wait_set_.end());
191 }
192
193 {
194 TRACE_EVENT0("impeller", "ClearSignaledFences");
195 // Erase the erased entries which will invoke callbacks.
196 erased_entries.clear(); // Bit redundant because of scope but hey.
197 }
198
199 return true;
200 }
201
202 void FenceWaiterVK::Terminate() {
203 {
204 std::scoped_lock lock(wait_set_mutex_);
205 terminate_ = true;
206 }
207 wait_set_cv_.notify_one();
208 }
209
210 } // namespace impeller
impeller::WaitSetEntry::Create
static std::shared_ptr< WaitSetEntry > Create(vk::UniqueFence p_fence, const fml::closure &p_callback)
Definition: fence_waiter_vk.cc:20
impeller::GetFencesForWaitSet
static std::vector< vk::Fence > GetFencesForWaitSet(const WaitSet &set)
Definition: fence_waiter_vk.cc:77
fence_waiter_vk.h
impeller::WaitSet
std::vector< std::shared_ptr< WaitSetEntry > > WaitSet
Definition: fence_waiter_vk.h:24
impeller::WaitSetEntry::GetFence
const vk::Fence & GetFence() const
Definition: fence_waiter_vk.cc:33
impeller::FenceWaiterVK::AddFence
bool AddFence(vk::UniqueFence fence, const fml::closure &callback)
Definition: fence_waiter_vk.cc:59
impeller::Main
bool Main(const fml::CommandLine &command_line)
Definition: blobcat_main.cc:13
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::WaitSetEntry
Definition: fence_waiter_vk.cc:18
std
Definition: comparable.h:98
impeller::FenceWaiterVK::Terminate
void Terminate()
Definition: fence_waiter_vk.cc:202
impeller::WaitSetEntry::IsSignalled
bool IsSignalled() const
Definition: fence_waiter_vk.cc:35
impeller::FenceWaiterVK::~FenceWaiterVK
~FenceWaiterVK()
Definition: fence_waiter_vk.cc:54
impeller::WaitSetEntry::UpdateSignalledStatus
void UpdateSignalledStatus(const vk::Device &device)
Definition: fence_waiter_vk.cc:26
impeller
Definition: aiks_context.cc:10