From 15798a885b9792de8162af78fd0df3bceaf4f1cd Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 24 Aug 2020 11:48:13 -0700 Subject: [PATCH] Fix race condition and data race in FrameInfoContainsValidWidthAndHeight. (flutter/engine#20706) The data race: gl_surface_fbo_frame_infos_ in the test context to was appended to on the raster task runner but read on the platform task runner. This is now sidestepped by using a callback and pushing that responsibility to the test. Setting the callback is guarded behind a mutex. The race condition: The assertions were previously run when the UI thread was done generating the frames. However, the assertions were run on the results collected on the raster thread in response the frame requests from UI thread. Just run the assertions on the raster thread directly. Fixes https://github.com/flutter/flutter/issues/64344 --- .../embedder/tests/embedder_test_context.cc | 27 ++++++++++++++----- .../embedder/tests/embedder_test_context.h | 20 +++++++++++--- .../embedder/tests/embedder_unittests.cc | 27 +++++++++---------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc index 048d5d9c14..9394f9f609 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc @@ -30,7 +30,9 @@ EmbedderTestContext::EmbedderTestContext(std::string assets_path) }); } -EmbedderTestContext::~EmbedderTestContext() = default; +EmbedderTestContext::~EmbedderTestContext() { + SetGLGetFBOCallback(nullptr); +} void EmbedderTestContext::SetupAOTMappingsIfNecessary() { if (!DartVM::IsRunningPrecompiledCode()) { @@ -197,14 +199,25 @@ bool EmbedderTestContext::GLPresent() { return true; } -uint32_t EmbedderTestContext::GLGetFramebuffer(FlutterFrameInfo frame_info) { - FML_CHECK(gl_surface_) << "GL surface must be initialized."; - gl_surface_fbo_frame_infos_.push_back(frame_info); - return gl_surface_->GetFramebuffer(); +void EmbedderTestContext::SetGLGetFBOCallback(GLGetFBOCallback callback) { + std::scoped_lock lock(gl_get_fbo_callback_mutex_); + gl_get_fbo_callback_ = callback; } -std::vector EmbedderTestContext::GetGLFBOFrameInfos() { - return gl_surface_fbo_frame_infos_; +uint32_t EmbedderTestContext::GLGetFramebuffer(FlutterFrameInfo frame_info) { + FML_CHECK(gl_surface_) << "GL surface must be initialized."; + + GLGetFBOCallback callback; + { + std::scoped_lock lock(gl_get_fbo_callback_mutex_); + callback = gl_get_fbo_callback_; + } + + if (callback) { + callback(frame_info); + } + + return gl_surface_->GetFramebuffer(); } bool EmbedderTestContext::GLMakeResourceCurrent() { diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h index f4135c07d7..c10bb9a67e 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -79,8 +80,20 @@ class EmbedderTestContext { size_t GetSoftwareSurfacePresentCount() const; - // Returns the frame information for all the frames that were rendered. - std::vector GetGLFBOFrameInfos(); + using GLGetFBOCallback = std::function; + + //---------------------------------------------------------------------------- + /// @brief Sets a callback that will be invoked (on the raster task + /// runner) when the engine asks the embedder for a new FBO ID at + /// the updated size. + /// + /// @attention The callback will be invoked on the raster task runner. The + /// callback can be set on the tests host thread. + /// + /// @param[in] callback The callback to set. The previous callback will be + /// un-registered. + /// + void SetGLGetFBOCallback(GLGetFBOCallback callback); private: // This allows the builder to access the hooks. @@ -104,9 +117,10 @@ class EmbedderTestContext { std::unique_ptr compositor_; NextSceneCallback next_scene_callback_; SkMatrix root_surface_transformation_; - std::vector gl_surface_fbo_frame_infos_; size_t gl_surface_present_count_ = 0; size_t software_surface_present_count_ = 0; + std::mutex gl_get_fbo_callback_mutex_; + GLGetFBOCallback gl_get_fbo_callback_; static VoidCallback GetIsolateCreateCallbackHook(); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index f235613698..18305b0ee7 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -4376,24 +4376,23 @@ TEST_F(EmbedderTest, FrameInfoContainsValidWidthAndHeight) { kSuccess); ASSERT_TRUE(engine.is_valid()); - constexpr size_t frames_expected = 10; - fml::CountDownLatch frame_latch(frames_expected); - size_t frames_seen = 0; + fml::CountDownLatch frame_latch(10); + context.AddNativeCallback("SignalNativeTest", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { - frames_seen++; - frame_latch.CountDown(); + /* Nothing to do. */ })); + + context.SetGLGetFBOCallback( + [&event, &frame_latch](FlutterFrameInfo frame_info) { + // width and height are rotated by 90 deg + ASSERT_EQ(frame_info.size.width, event.height); + ASSERT_EQ(frame_info.size.height, event.width); + + frame_latch.CountDown(); + }); + frame_latch.Wait(); - - ASSERT_EQ(frames_expected, frames_seen); - ASSERT_EQ(context.GetGLFBOFrameInfos().size(), frames_seen); - - for (FlutterFrameInfo frame_info : context.GetGLFBOFrameInfos()) { - // width and height are rotated by 90 deg - ASSERT_EQ(frame_info.size.width, event.height); - ASSERT_EQ(frame_info.size.height, event.width); - } } TEST_F(EmbedderTest, MustNotRunWithBothFBOCallbacksSet) {