Skip to content

Commit cfbf8e0

Browse files
leehacksuryaKommana2662
authored andcommitted
[Impeller] Guard the sampler library caches with a mutex (flutter#193738)
`SamplerLibraryMTL`, `SamplerLibraryVK` and `SamplerLibraryGLES` cache samplers in a plain vector that `GetSampler` scans and then appends to, with no lock. Flutter GPU calls `GetSampler` on the UI thread (`RenderPass.bindTexture`) while Impeller calls it on the raster thread (for example `FramebufferBlendContents::Render`). A scan can read a buffer that a concurrent `push_back` is reallocating, and two concurrent inserts can corrupt the vector or drop a sampler that was already returned. This PR guards `samplers_` with a mutex in all three backends. On Metal and Vulkan, the lock covers the lookup and the insert but not sampler creation, and the insert checks the cache again so concurrent requests share one sampler. GLES creates no device object, so it holds the lock for the whole lookup. The new test, `RendererTest.SamplerLibraryAllowsConcurrentGetSampler`, starts four threads on a latch, has each request the same 36 samplers, and checks that every thread gets the same valid sampler for each descriptor. Verified on master d03768e: Metal on macOS (M4 Max), and Vulkan (SwiftShader) and OpenGL ES (Mesa, Xvfb) on Linux x64 with the CI environment: | | Without the lock | With the lock | | --- | --- | --- | | New test, 40 runs (Metal / Vulkan / GLES) | 0 / 1 / 0 pass | 40/40 on each | | New test under TSan | sampler races on all three | no sampler race | | Repro app from the issue under TSan (Metal) | race in 4 of 6 runs | none in 6 runs | The macOS `impeller_unittests` CI filter (2426 passed, 0 failed) and the Metal backend tests pass, and the format and clang-tidy checks are clean. On Linux, the full `impeller_unittests` run was stopped early with 1154 passed and 0 failed, and the runs used the previous revision of the test, which started its threads with a spin barrier instead of a latch. Related: flutter#193721 / flutter#193722 (the same race in `PipelineLibraryMTL`). Both PRs add a test to `renderer_unittests.cc`; merging them conflicts only on the `#include <...>` lines. Fixes flutter#193737 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 6999731 commit cfbf8e0

7 files changed

Lines changed: 122 additions & 12 deletions

File tree

‎engine/src/flutter/impeller/renderer/backend/gles/sampler_library_gles.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ raw_ptr<const Sampler> SamplerLibraryGLES::GetSampler(
3131
return raw_ptr<const Sampler>{nullptr};
3232
}
3333
uint64_t p_key = SamplerDescriptor::ToKey(descriptor);
34+
Lock lock(samplers_mutex_);
3435
for (const auto& [key, value] : samplers_) {
3536
if (key == p_key) {
3637
return raw_ptr(value);

‎engine/src/flutter/impeller/renderer/backend/gles/sampler_library_gles.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_SAMPLER_LIBRARY_GLES_H_
66
#define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_SAMPLER_LIBRARY_GLES_H_
77

8+
#include "impeller/base/thread.h"
89
#include "impeller/core/sampler.h"
910
#include "impeller/core/sampler_descriptor.h"
1011
#include "impeller/renderer/backend/gles/sampler_gles.h"
@@ -21,7 +22,9 @@ class SamplerLibraryGLES final : public SamplerLibrary {
2122
private:
2223
friend class ContextGLES;
2324

24-
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_;
25+
Mutex samplers_mutex_;
26+
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_
27+
IPLR_GUARDED_BY(samplers_mutex_);
2528

2629
SamplerLibraryGLES();
2730

‎engine/src/flutter/impeller/renderer/backend/metal/sampler_library_mtl.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "impeller/base/backend_cast.h"
1313
#include "impeller/base/comparable.h"
14+
#include "impeller/base/thread.h"
1415
#include "impeller/core/sampler_descriptor.h"
1516
#include "impeller/renderer/sampler_library.h"
1617

@@ -27,14 +28,25 @@ class SamplerLibraryMTL final
2728
friend class ContextMTL;
2829

2930
id<MTLDevice> device_ = nullptr;
30-
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_;
31+
Mutex samplers_mutex_;
32+
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_
33+
IPLR_GUARDED_BY(samplers_mutex_);
3134

3235
explicit SamplerLibraryMTL(id<MTLDevice> device);
3336

3437
// |SamplerLibrary|
3538
raw_ptr<const Sampler> GetSampler(
3639
const SamplerDescriptor& descriptor) override;
3740

41+
/// @brief Find the cached sampler for `key`, or a null `raw_ptr` if there
42+
/// is none.
43+
///
44+
/// The result outlives `samplers_mutex_`: `samplers_` holds each
45+
/// sampler by shared_ptr and never removes one, so it stays valid
46+
/// for the lifetime of this library.
47+
raw_ptr<const Sampler> FindSampler(uint64_t key) const
48+
IPLR_REQUIRES(samplers_mutex_);
49+
3850
SamplerLibraryMTL(const SamplerLibraryMTL&) = delete;
3951

4052
SamplerLibraryMTL& operator=(const SamplerLibraryMTL&) = delete;

‎engine/src/flutter/impeller/renderer/backend/metal/sampler_library_mtl.mm‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
raw_ptr<const Sampler> SamplerLibraryMTL::GetSampler(
1919
const SamplerDescriptor& descriptor) {
2020
uint64_t p_key = SamplerDescriptor::ToKey(descriptor);
21-
for (const auto& [key, value] : samplers_) {
22-
if (key == p_key) {
23-
return raw_ptr(value);
21+
{
22+
Lock lock(samplers_mutex_);
23+
if (auto sampler = FindSampler(p_key)) {
24+
return sampler;
2425
}
2526
}
2627
if (!device_) {
@@ -52,8 +53,21 @@
5253
}
5354
auto sampler =
5455
std::shared_ptr<SamplerMTL>(new SamplerMTL(descriptor, mtl_sampler));
56+
Lock lock(samplers_mutex_);
57+
if (auto existing = FindSampler(p_key)) {
58+
return existing;
59+
}
5560
samplers_.push_back(std::make_pair(p_key, std::move(sampler)));
5661
return raw_ptr(samplers_.back().second);
5762
}
5863

64+
raw_ptr<const Sampler> SamplerLibraryMTL::FindSampler(uint64_t key) const {
65+
for (const auto& [sampler_key, sampler] : samplers_) {
66+
if (sampler_key == key) {
67+
return raw_ptr(sampler);
68+
}
69+
}
70+
return raw_ptr<const Sampler>(nullptr);
71+
}
72+
5973
} // namespace impeller

‎engine/src/flutter/impeller/renderer/backend/vulkan/sampler_library_vk.cc‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,33 @@ raw_ptr<const Sampler> SamplerLibraryVK::GetSampler(
3030
std::max<uint32_t>(1u, max_sampler_anisotropy_)));
3131

3232
uint64_t p_key = SamplerDescriptor::ToKey(desc_copy);
33-
for (const auto& [key, value] : samplers_) {
34-
if (key == p_key) {
35-
return raw_ptr(value);
33+
{
34+
Lock lock(samplers_mutex_);
35+
if (auto sampler = FindSampler(p_key)) {
36+
return sampler;
3637
}
3738
}
3839
auto device_holder = device_holder_.lock();
3940
if (!device_holder || !device_holder->GetDevice()) {
4041
return raw_ptr<const Sampler>(nullptr);
4142
}
42-
samplers_.push_back(std::make_pair(
43-
p_key,
44-
std::make_shared<SamplerVK>(device_holder->GetDevice(), desc_copy)));
43+
auto sampler =
44+
std::make_shared<SamplerVK>(device_holder->GetDevice(), desc_copy);
45+
Lock lock(samplers_mutex_);
46+
if (auto existing = FindSampler(p_key)) {
47+
return existing;
48+
}
49+
samplers_.push_back(std::make_pair(p_key, std::move(sampler)));
4550
return raw_ptr(samplers_.back().second);
4651
}
4752

53+
raw_ptr<const Sampler> SamplerLibraryVK::FindSampler(uint64_t key) const {
54+
for (const auto& [sampler_key, sampler] : samplers_) {
55+
if (sampler_key == key) {
56+
return raw_ptr(sampler);
57+
}
58+
}
59+
return raw_ptr<const Sampler>(nullptr);
60+
}
61+
4862
} // namespace impeller

‎engine/src/flutter/impeller/renderer/backend/vulkan/sampler_library_vk.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_SAMPLER_LIBRARY_VK_H_
77

88
#include "impeller/base/backend_cast.h"
9+
#include "impeller/base/thread.h"
910
#include "impeller/core/sampler.h"
1011
#include "impeller/core/sampler_descriptor.h"
1112
#include "impeller/renderer/backend/vulkan/device_holder_vk.h"
@@ -27,13 +28,24 @@ class SamplerLibraryVK final
2728
friend class ContextVK;
2829

2930
std::weak_ptr<DeviceHolderVK> device_holder_;
30-
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_;
31+
Mutex samplers_mutex_;
32+
std::vector<std::pair<uint64_t, std::shared_ptr<const Sampler>>> samplers_
33+
IPLR_GUARDED_BY(samplers_mutex_);
3134
uint32_t max_sampler_anisotropy_ = 1;
3235

3336
// |SamplerLibrary|
3437
raw_ptr<const Sampler> GetSampler(
3538
const SamplerDescriptor& descriptor) override;
3639

40+
/// @brief Find the cached sampler for `key`, or a null `raw_ptr` if there
41+
/// is none.
42+
///
43+
/// The result outlives `samplers_mutex_`: `samplers_` holds each
44+
/// sampler by shared_ptr and never removes one, so it stays valid
45+
/// for the lifetime of this library.
46+
raw_ptr<const Sampler> FindSampler(uint64_t key) const
47+
IPLR_REQUIRES(samplers_mutex_);
48+
3749
SamplerLibraryVK(const SamplerLibraryVK&) = delete;
3850

3951
SamplerLibraryVK& operator=(const SamplerLibraryVK&) = delete;

‎engine/src/flutter/impeller/renderer/renderer_unittests.cc‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <thread>
88

99
#include "flutter/fml/logging.h"
10+
#include "flutter/fml/synchronization/count_down_latch.h"
1011
#include "flutter/fml/time/time_point.h"
1112
#include "impeller/base/validation.h"
1213
#include "impeller/core/device_buffer_descriptor.h"
@@ -1806,6 +1807,59 @@ TEST_P(RendererTest, AttachmentRejectsOutOfRangeSubresource) {
18061807
EXPECT_FALSE(color0.IsValid());
18071808
}
18081809

1810+
TEST_P(RendererTest, SamplerLibraryAllowsConcurrentGetSampler) {
1811+
auto library = GetContext()->GetSamplerLibrary();
1812+
ASSERT_TRUE(library);
1813+
1814+
std::vector<SamplerDescriptor> descriptors;
1815+
for (auto min : {MinMagFilter::kNearest, MinMagFilter::kLinear}) {
1816+
for (auto mag : {MinMagFilter::kNearest, MinMagFilter::kLinear}) {
1817+
for (auto mip :
1818+
{MipFilter::kBase, MipFilter::kNearest, MipFilter::kLinear}) {
1819+
for (auto mode :
1820+
{SamplerAddressMode::kClampToEdge, SamplerAddressMode::kRepeat,
1821+
SamplerAddressMode::kMirror}) {
1822+
SamplerDescriptor desc("Concurrent", min, mag, mip);
1823+
desc.width_address_mode = mode;
1824+
desc.height_address_mode = mode;
1825+
descriptors.push_back(desc);
1826+
}
1827+
}
1828+
}
1829+
}
1830+
1831+
constexpr size_t kThreadCount = 4;
1832+
std::vector<std::vector<const Sampler*>> results(
1833+
kThreadCount, std::vector<const Sampler*>(descriptors.size()));
1834+
fml::CountDownLatch start(kThreadCount);
1835+
std::vector<std::thread> threads;
1836+
threads.reserve(kThreadCount);
1837+
for (size_t t = 0; t < kThreadCount; t++) {
1838+
threads.emplace_back([&, t] {
1839+
start.CountDown();
1840+
start.Wait();
1841+
for (size_t i = 0; i < descriptors.size(); i++) {
1842+
size_t index =
1843+
(i + t * descriptors.size() / kThreadCount) % descriptors.size();
1844+
results[t][index] = library->GetSampler(descriptors[index]).get();
1845+
}
1846+
});
1847+
}
1848+
for (auto& thread : threads) {
1849+
thread.join();
1850+
}
1851+
1852+
for (size_t i = 0; i < descriptors.size(); i++) {
1853+
const Sampler* sampler = library->GetSampler(descriptors[i]).get();
1854+
ASSERT_NE(sampler, nullptr);
1855+
EXPECT_EQ(SamplerDescriptor::ToKey(sampler->GetDescriptor()),
1856+
SamplerDescriptor::ToKey(descriptors[i]));
1857+
for (size_t t = 0; t < kThreadCount; t++) {
1858+
EXPECT_EQ(results[t][i], sampler);
1859+
}
1860+
}
1861+
}
1862+
18091863
} // namespace testing
18101864
} // namespace impeller
18111865

0 commit comments

Comments
 (0)