Skip to content

Commit 331c569

Browse files
[CP-stable][Impeller] Make ContextVK hash values globally unique (#171239)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: What is the link to the issue this cherry-pick is addressing? #170141 ### Changelog Description: Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples Fixes an issue in the Impeller Vulkan back end that can cause crashes when transitioning between different Android activities that use Flutter. ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch) Potential crash in apps with multiple Android activities running Flutter with Impeller/Vulkan. ### Workaround: Is there a workaround for this issue? Disable Impeller ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? The `appShortcutLaunchActivityAfterStarting` test for the `quick_actions_android` plugin will no longer crash on Firebase Test Lab after this fix is applied. I don't know of a simpler way to manually verify the fix.
1 parent d779fc7 commit 331c569

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ size_t ContextVK::ChooseThreadCountForWorkers(size_t hardware_concurrency) {
119119
}
120120

121121
namespace {
122-
thread_local uint64_t tls_context_count = 0;
122+
std::atomic_uint64_t context_count = 0;
123123
uint64_t CalculateHash(void* ptr) {
124-
// You could make a context once per nanosecond for 584 years on one thread
125-
// before this overflows.
126-
return ++tls_context_count;
124+
return context_count.fetch_add(1);
127125
}
128126
} // namespace
129127

engine/src/flutter/impeller/renderer/backend/vulkan/context_vk_unittests.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,5 +392,21 @@ TEST(ContextVKTest, AHBSwapchainCapabilitiesCanBeMissing) {
392392

393393
} // namespace impeller
394394

395+
TEST(ContextVKTest, HashIsUniqueAcrossThreads) {
396+
uint64_t hash1, hash2;
397+
std::thread thread1([&]() {
398+
auto context = MockVulkanContextBuilder().Build();
399+
hash1 = context->GetHash();
400+
});
401+
std::thread thread2([&]() {
402+
auto context = MockVulkanContextBuilder().Build();
403+
hash2 = context->GetHash();
404+
});
405+
thread1.join();
406+
thread2.join();
407+
408+
EXPECT_NE(hash1, hash2);
409+
}
410+
395411
} // namespace testing
396412
} // namespace impeller

0 commit comments

Comments
 (0)