| Flutter Impeller |

resource_manager_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/resource_manager_vk.h"
6
7 #include "flutter/fml/cpu_affinity.h"
8 #include "flutter/fml/thread.h"
9 #include "flutter/fml/trace_event.h"
10 #include "fml/logging.h"
11
12 namespace impeller {
13
14 std::shared_ptr<ResourceManagerVK> ResourceManagerVK::Create() {
15 // It will be tempting to refactor this to create the waiter thread in the
16 // static method instead of the constructor. However, that causes the
17 // destructor never to be called, and the thread never terminates!
18 //
19 // See https://github.com/flutter/flutter/issues/134482.
20 return std::shared_ptr<ResourceManagerVK>(new ResourceManagerVK());
21 }
22
23 ResourceManagerVK::ResourceManagerVK() : waiter_([&]() { Start(); }) {}
24
25 ResourceManagerVK::~ResourceManagerVK() {
26 FML_DCHECK(waiter_.get_id() != std::this_thread::get_id())
27 << "The ResourceManager being destructed on its own spawned thread is a "
28 << "sign that ContextVK was not properly destroyed. A usual fix for this "
29 << "is to ensure that ContextVK is shutdown (i.e. context->Shutdown()) "
30 "before the ResourceManager is destroyed (i.e. at the end of a test).";
31 Terminate();
32 waiter_.join();
33 }
34
35 void ResourceManagerVK::Start() {
36 // It's possible for Start() to be called when terminating:
37 // { ResourceManagerVK::Create(); }
38 //
39 // ... so no FML_DCHECK here.
40
41 fml::Thread::SetCurrentThreadName(
42 fml::Thread::ThreadConfig{"io.flutter.impeller.resource_manager"});
43 // While this code calls destructors it doesn't need to be particularly fast
44 // with them, as long as it doesn't interrupt raster thread.
45 fml::RequestAffinity(fml::CpuAffinity::kEfficiency);
46
47 bool should_exit = false;
48 while (!should_exit) {
49 std::unique_lock lock(reclaimables_mutex_);
50
51 // Wait until there are reclaimable resource or if the manager should be
52 // torn down.
53 reclaimables_cv_.wait(
54 lock, [&]() { return !reclaimables_.empty() || should_exit_; });
55
56 // Don't reclaim resources when the lock is being held as this may gate
57 // further reclaimables from being registered.
58 Reclaimables resources_to_collect;
59 std::swap(resources_to_collect, reclaimables_);
60
61 // We can't read the ivar outside the lock. Read it here instead.
62 should_exit = should_exit_;
63
64 // We know what to collect. Unlock before doing anything else.
65 lock.unlock();
66
67 // Claim all resources while tracing.
68 {
69 TRACE_EVENT0("Impeller", "ReclaimResources");
70 resources_to_collect.clear(); // Redundant because of scope but here so
71 // we can add a trace around it.
72 }
73 }
74 }
75
76 void ResourceManagerVK::Reclaim(std::unique_ptr<ResourceVK> resource) {
77 if (!resource) {
78 return;
79 }
80 {
81 std::scoped_lock lock(reclaimables_mutex_);
82 reclaimables_.emplace_back(std::move(resource));
83 }
84 reclaimables_cv_.notify_one();
85 }
86
87 void ResourceManagerVK::Terminate() {
88 // The thread should not be terminated more than once.
89 FML_DCHECK(!should_exit_);
90
91 {
92 std::scoped_lock lock(reclaimables_mutex_);
93 should_exit_ = true;
94 }
95 reclaimables_cv_.notify_one();
96 }
97
98 } // namespace impeller
impeller::ResourceManagerVK::Create
static std::shared_ptr< ResourceManagerVK > Create()
Creates a shared resource manager (a dedicated thread).
Definition: resource_manager_vk.cc:14
impeller::ResourceManagerVK
A resource manager controls how resources are allocated and reclaimed.
Definition: resource_manager_vk.h:39
impeller::ResourceManagerVK::Reclaim
void Reclaim(std::unique_ptr< ResourceVK > resource)
Mark a resource as being reclaimable.
Definition: resource_manager_vk.cc:76
resource_manager_vk.h
impeller::ResourceManagerVK::~ResourceManagerVK
~ResourceManagerVK()
Destroys the resource manager.
Definition: resource_manager_vk.cc:25
impeller
Definition: aiks_context.cc:10