From 98718ceb9b2e91a3e47881844e2852d6c57b9600 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 26 Apr 2023 10:23:28 -0700 Subject: [PATCH] [Impeller] removed collisions of dead command pools between tests. (flutter/engine#41490) fixes https://github.com/flutter/flutter/issues/125321 Tests coverage from existing tests (see issue). ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../renderer/backend/vulkan/command_pool_vk.cc | 10 ++++++---- .../impeller/renderer/backend/vulkan/context_vk.cc | 11 ++++++++++- .../impeller/renderer/backend/vulkan/context_vk.h | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc index 0ccac93548..46d48c3185 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc @@ -14,9 +14,11 @@ namespace impeller { -using CommandPoolMap = - std::map>; +using CommandPoolMap = std::map>; +// TODO(tbd): This is storing tons of CommandPoolVK's in the test runner since +// many contexts are created on the same thread. We need to come up +// with a different way to clean these up. FML_THREAD_LOCAL fml::ThreadLocalUniquePtr tls_command_pool; static Mutex g_all_pools_mutex; @@ -33,7 +35,7 @@ std::shared_ptr CommandPoolVK::GetThreadLocal( tls_command_pool.reset(new CommandPoolMap()); } CommandPoolMap& pool_map = *tls_command_pool.get(); - auto found = pool_map.find(context); + auto found = pool_map.find(context->GetHash()); if (found != pool_map.end() && found->second->IsValid()) { return found->second; } @@ -41,7 +43,7 @@ std::shared_ptr CommandPoolVK::GetThreadLocal( if (!pool->IsValid()) { return nullptr; } - pool_map[context] = pool; + pool_map[context->GetHash()] = pool; { Lock pool_lock(g_all_pools_mutex); g_all_pools[context].push_back(pool); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc index 157aa76f63..d41ccd0a5f 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -95,7 +95,16 @@ std::shared_ptr ContextVK::Create(Settings settings) { return context; } -ContextVK::ContextVK() = default; +namespace { +thread_local uint64_t tls_context_count = 0; +uint64_t CalculateHash(void* ptr) { + // You could make a context once per nanosecond for 584 years on one thread + // before this overflows. + return ++tls_context_count; +} +} // namespace + +ContextVK::ContextVK() : hash_(CalculateHash(this)) {} ContextVK::~ContextVK() { if (device_) { diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h index 9b95214f83..cc1376649b 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h @@ -46,6 +46,8 @@ class ContextVK final : public Context, public BackendCast { static std::shared_ptr Create(Settings settings); + uint64_t GetHash() const { return hash_; } + // |Context| ~ContextVK() override; @@ -134,6 +136,7 @@ class ContextVK final : public Context, public BackendCast { std::shared_ptr device_capabilities_; std::shared_ptr fence_waiter_; std::string device_name_; + const uint64_t hash_; bool is_valid_ = false;