Skip to content

Commit 32c15a3

Browse files
bderovia-guy
authored andcommitted
[Impeller] Fix dirty-range race in DeviceBufferGLES uploads (flutter#187932)
Fixes flutter#187931. `DeviceBufferGLES::Flush()` merges dirty ranges from writer threads (for example the UI thread writing to a flutter_gpu host-visible buffer) while `BindAndUploadDataIfNecessary()` consumes the range on a reactor thread. The upload read the dirty range, called `glBufferSubData`, and cleared `dirty_range_` afterwards, so any flush landing during the upload was silently discarded and its data never reached the GPU. Draws could then read stale vertex/instance/uniform data, and when a buffer's only flush was lost, uninitialized buffer contents. This change guards `dirty_range_` with a mutex and takes and clears it before uploading, so a concurrent flush merges into a fresh range that the next bind uploads. Also adds `BufferSubData` to the GLES test mocks and a regression test that flushes from inside the mocked upload, which fails with the previous ordering. ## 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 documentation (doc comments with `///`). - [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/ [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 50dd151 commit 32c15a3

5 files changed

Lines changed: 84 additions & 6 deletions

File tree

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ std::optional<GLuint> DeviceBufferGLES::GetHandle() const {
6363
}
6464

6565
void DeviceBufferGLES::Flush(std::optional<Range> range) const {
66+
Lock lock(dirty_range_mutex_);
6667
if (!range.has_value()) {
6768
dirty_range_ = Range{
6869
0, static_cast<size_t>(backing_store_->GetLength().GetByteSize())};
@@ -116,11 +117,18 @@ bool DeviceBufferGLES::BindAndUploadDataIfNecessary(BindingType type) const {
116117
initialized_ = true;
117118
}
118119

119-
if (dirty_range_.has_value()) {
120-
auto range = dirty_range_.value();
121-
gl.BufferSubData(target_type, range.offset, range.length,
122-
backing_store_->GetBuffer() + range.offset);
123-
dirty_range_ = std::nullopt;
120+
// Take and clear the dirty range BEFORE uploading. A Flush() from another
121+
// thread during the upload then merges into a fresh dirty range that the
122+
// next bind uploads, instead of being silently discarded by a clear that
123+
// runs after the upload.
124+
std::optional<Range> dirty;
125+
{
126+
Lock lock(dirty_range_mutex_);
127+
std::swap(dirty_range_, dirty);
128+
}
129+
if (dirty.has_value()) {
130+
gl.BufferSubData(target_type, dirty->offset, dirty->length,
131+
backing_store_->GetBuffer() + dirty->offset);
124132
}
125133

126134
return true;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "impeller/base/allocation.h"
1212
#include "impeller/base/backend_cast.h"
13+
#include "impeller/base/thread.h"
1314
#include "impeller/core/device_buffer.h"
1415
#include "impeller/renderer/backend/gles/reactor_gles.h"
1516

@@ -49,7 +50,12 @@ class DeviceBufferGLES final
4950
// Mutable for lazy evaluation.
5051
mutable std::optional<HandleGLES> handle_;
5152
mutable std::unique_ptr<Allocation> backing_store_;
52-
mutable std::optional<Range> dirty_range_ = std::nullopt;
53+
// Guards dirty_range_. Flush() merges ranges from writer threads (e.g. the
54+
// UI thread for flutter_gpu host-visible buffers) while
55+
// BindAndUploadDataIfNecessary() consumes the range on a reactor thread.
56+
mutable Mutex dirty_range_mutex_;
57+
mutable std::optional<Range> dirty_range_
58+
IPLR_GUARDED_BY(dirty_range_mutex_) = std::nullopt;
5359
mutable bool initialized_ = false;
5460

5561
// |DeviceBuffer|

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,49 @@ class TestWorker : public ReactorGLES::Worker {
2222
};
2323
} // namespace
2424

25+
// A Flush() that lands while BindAndUploadDataIfNecessary is mid-upload (as
26+
// happens when another thread writes to a host-visible buffer during the GL
27+
// upload) must not be discarded; the next bind must upload the new range.
28+
TEST(DeviceBufferGLESTest, FlushDuringUploadIsNotDiscarded) {
29+
auto mock_gles_impl = std::make_unique<MockGLESImpl>();
30+
31+
DeviceBufferGLES* device_buffer_ptr = nullptr;
32+
int upload_count = 0;
33+
EXPECT_CALL(*mock_gles_impl, BufferSubData(_, _, _, _))
34+
.Times(2)
35+
.WillRepeatedly([&](GLenum target, GLintptr offset, GLsizeiptr size,
36+
const void* data) {
37+
++upload_count;
38+
if (upload_count == 1) {
39+
// Simulates a writer thread flushing new data during the upload.
40+
device_buffer_ptr->Flush(Range{0, 4});
41+
}
42+
});
43+
44+
std::shared_ptr<MockGLES> mock_gles =
45+
MockGLES::Init(std::move(mock_gles_impl));
46+
ProcTableGLES::Resolver resolver = kMockResolverGLES;
47+
auto proc_table = std::make_unique<ProcTableGLES>(resolver);
48+
auto worker = std::make_shared<TestWorker>();
49+
auto reactor = std::make_shared<ReactorGLES>(std::move(proc_table));
50+
reactor->AddWorker(worker);
51+
52+
auto backing_store = std::make_unique<Allocation>();
53+
ASSERT_TRUE(backing_store->Truncate(Bytes{16}));
54+
DeviceBufferGLES device_buffer(DeviceBufferDescriptor{.size = 16}, reactor,
55+
std::move(backing_store));
56+
device_buffer_ptr = &device_buffer;
57+
58+
device_buffer.Flush(Range{0, 4});
59+
// First bind uploads the flushed range; the mock flushes again mid-upload.
60+
EXPECT_TRUE(device_buffer.BindAndUploadDataIfNecessary(
61+
DeviceBufferGLES::BindingType::kArrayBuffer));
62+
// Second bind must see the mid-upload flush and upload again.
63+
EXPECT_TRUE(device_buffer.BindAndUploadDataIfNecessary(
64+
DeviceBufferGLES::BindingType::kArrayBuffer));
65+
EXPECT_EQ(upload_count, 2);
66+
}
67+
2568
TEST(DeviceBufferGLESTest, BindUniformData) {
2669
auto mock_gles_impl = std::make_unique<MockGLESImpl>();
2770

‎engine/src/flutter/impeller/renderer/backend/gles/test/mock_gles.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ void mockGenBuffers(GLsizei n, GLuint* buffers) {
244244
CallMockMethod(&IMockGLESImpl::GenBuffers, n, buffers);
245245
}
246246

247+
void mockBufferSubData(GLenum target,
248+
GLintptr offset,
249+
GLsizeiptr size,
250+
const void* data) {
251+
CallMockMethod(&IMockGLESImpl::BufferSubData, target, offset, size, data);
252+
}
253+
254+
static_assert(CheckSameSignature<decltype(mockBufferSubData), //
255+
decltype(glBufferSubData)>::value);
256+
247257
static_assert(CheckSameSignature<decltype(mockGenTextures), //
248258
decltype(glGenTextures)>::value);
249259

@@ -499,6 +509,8 @@ const ProcTableGLES::Resolver kMockResolverGLES = [](const char* name) {
499509
return reinterpret_cast<void*>(mockObjectLabelKHR);
500510
} else if (strcmp(name, "glGenBuffers") == 0) {
501511
return reinterpret_cast<void*>(mockGenBuffers);
512+
} else if (strcmp(name, "glBufferSubData") == 0) {
513+
return reinterpret_cast<void*>(mockBufferSubData);
502514
} else if (strcmp(name, "glIsTexture") == 0) {
503515
return reinterpret_cast<void*>(mockIsTexture);
504516
} else if (strcmp(name, "glCheckFramebufferStatus") == 0) {

‎engine/src/flutter/impeller/renderer/backend/gles/test/mock_gles.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ class IMockGLESImpl {
9797
virtual void DeleteQueriesEXT(GLsizei size, const GLuint* queries) {}
9898
virtual void GenBuffers(GLsizei n, GLuint* buffers) {}
9999
virtual void DeleteBuffers(GLsizei n, const GLuint* buffers) {}
100+
virtual void BufferSubData(GLenum target,
101+
GLintptr offset,
102+
GLsizeiptr size,
103+
const void* data) {}
100104
virtual GLboolean IsTexture(GLuint texture) { return true; }
101105
virtual void DiscardFramebufferEXT(GLenum target,
102106
GLsizei numAttachments,
@@ -248,6 +252,11 @@ class MockGLESImpl : public IMockGLESImpl {
248252
DeleteBuffers,
249253
(GLsizei n, const GLuint* buffers),
250254
(override));
255+
MOCK_METHOD(
256+
void,
257+
BufferSubData,
258+
(GLenum target, GLintptr offset, GLsizeiptr size, const void* data),
259+
(override));
251260
MOCK_METHOD(GLboolean, IsTexture, (GLuint texture), (override));
252261
MOCK_METHOD(void,
253262
DiscardFramebufferEXT,

0 commit comments

Comments
 (0)