From b1ad898b4e96b791ba27e0450d564ee274e412d1 Mon Sep 17 00:00:00 2001 From: ColdPaleLight <31977171+ColdPaleLight@users.noreply.github.com> Date: Sat, 4 Jun 2022 01:18:06 +0800 Subject: [PATCH] Correct the frame timing in 'Rasterizer::Draw' when pipeline is more available (flutter/engine#32283) --- engine/src/flutter/shell/common/animator.cc | 9 +- engine/src/flutter/shell/common/animator.h | 5 +- .../shell/common/animator_unittests.cc | 6 +- engine/src/flutter/shell/common/pipeline.h | 13 ++ engine/src/flutter/shell/common/rasterizer.cc | 32 +-- engine/src/flutter/shell/common/rasterizer.h | 7 +- .../shell/common/rasterizer_unittests.cc | 192 ++++++++++++++---- engine/src/flutter/shell/common/shell.cc | 14 +- engine/src/flutter/shell/common/shell.h | 4 +- 9 files changed, 193 insertions(+), 89 deletions(-) diff --git a/engine/src/flutter/shell/common/animator.cc b/engine/src/flutter/shell/common/animator.cc index 8da760bc7e..31bd0cdfb5 100644 --- a/engine/src/flutter/shell/common/animator.cc +++ b/engine/src/flutter/shell/common/animator.cc @@ -163,26 +163,25 @@ void Animator::Render(std::unique_ptr layer_tree) { delegate_.OnAnimatorUpdateLatestFrameTargetTime( frame_timings_recorder_->GetVsyncTargetTime()); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), std::move(frame_timings_recorder_)); // Commit the pending continuation. PipelineProduceResult result = - producer_continuation_.Complete(std::move(layer_tree)); + producer_continuation_.Complete(std::move(layer_tree_item)); if (!result.success) { - frame_timings_recorder_.reset(); FML_DLOG(INFO) << "No pending continuation to commit"; return; } if (!result.is_first_item) { - frame_timings_recorder_.reset(); // It has been successfully pushed to the pipeline but not as the first // item. Eventually the 'Rasterizer' will consume it, so we don't need to // notify the delegate. return; } - delegate_.OnAnimatorDraw(layer_tree_pipeline_, - std::move(frame_timings_recorder_)); + delegate_.OnAnimatorDraw(layer_tree_pipeline_); } const std::weak_ptr Animator::GetVsyncWaiter() const { diff --git a/engine/src/flutter/shell/common/animator.h b/engine/src/flutter/shell/common/animator.h index 4acf86ea48..d822935307 100644 --- a/engine/src/flutter/shell/common/animator.h +++ b/engine/src/flutter/shell/common/animator.h @@ -40,8 +40,7 @@ class Animator final { fml::TimePoint frame_target_time) = 0; virtual void OnAnimatorDraw( - std::shared_ptr> pipeline, - std::unique_ptr frame_timings_recorder) = 0; + std::shared_ptr pipeline) = 0; virtual void OnAnimatorDrawLastLayerTree( std::unique_ptr frame_timings_recorder) = 0; @@ -83,8 +82,6 @@ class Animator final { void EnqueueTraceFlowId(uint64_t trace_flow_id); private: - using LayerTreePipeline = Pipeline; - void BeginFrame(std::unique_ptr frame_timings_recorder); bool CanReuseLastLayerTree(); diff --git a/engine/src/flutter/shell/common/animator_unittests.cc b/engine/src/flutter/shell/common/animator_unittests.cc index 32ed28c61c..628d7eb9af 100644 --- a/engine/src/flutter/shell/common/animator_unittests.cc +++ b/engine/src/flutter/shell/common/animator_unittests.cc @@ -35,10 +35,8 @@ class FakeAnimatorDelegate : public Animator::Delegate { MOCK_METHOD1(OnAnimatorUpdateLatestFrameTargetTime, void(fml::TimePoint frame_target_time)); - MOCK_METHOD2( - OnAnimatorDraw, - void(std::shared_ptr> pipeline, - std::unique_ptr frame_timings_recorder)); + MOCK_METHOD1(OnAnimatorDraw, + void(std::shared_ptr pipeline)); void OnAnimatorDrawLastLayerTree( std::unique_ptr frame_timings_recorder) override {} diff --git a/engine/src/flutter/shell/common/pipeline.h b/engine/src/flutter/shell/common/pipeline.h index 44090aaf49..35f90f39f2 100644 --- a/engine/src/flutter/shell/common/pipeline.h +++ b/engine/src/flutter/shell/common/pipeline.h @@ -9,6 +9,8 @@ #include #include +#include "flutter/flow/frame_timings.h" +#include "flutter/flow/layers/layer_tree.h" #include "flutter/fml/macros.h" #include "flutter/fml/memory/ref_counted.h" #include "flutter/fml/synchronization/semaphore.h" @@ -219,6 +221,17 @@ class Pipeline { FML_DISALLOW_COPY_AND_ASSIGN(Pipeline); }; +struct LayerTreeItem { + LayerTreeItem(std::unique_ptr layer_tree, + std::unique_ptr frame_timings_recorder) + : layer_tree(std::move(layer_tree)), + frame_timings_recorder(std::move(frame_timings_recorder)) {} + std::unique_ptr layer_tree; + std::unique_ptr frame_timings_recorder; +}; + +using LayerTreePipeline = Pipeline; + } // namespace flutter #endif // FLUTTER_SHELL_COMMON_PIPELINE_H_ diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index 1a26ee0879..0b2b1a301d 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -160,12 +160,9 @@ void Rasterizer::DrawLastLayerTree( } } -RasterStatus Rasterizer::Draw( - std::unique_ptr frame_timings_recorder, - std::shared_ptr> pipeline, - LayerTreeDiscardCallback discard_callback) { - TRACE_EVENT_WITH_FRAME_NUMBER(frame_timings_recorder, "flutter", - "GPURasterizer::Draw"); +RasterStatus Rasterizer::Draw(std::shared_ptr pipeline, + LayerTreeDiscardCallback discard_callback) { + TRACE_EVENT0("flutter", "GPURasterizer::Draw"); if (raster_thread_merger_ && !raster_thread_merger_->IsOnRasterizingThread()) { // we yield and let this frame be serviced on the right thread. @@ -175,13 +172,12 @@ RasterStatus Rasterizer::Draw( .GetRasterTaskRunner() ->RunsTasksOnCurrentThread()); - std::unique_ptr resubmit_recorder = - frame_timings_recorder->CloneUntil( - FrameTimingsRecorder::State::kBuildEnd); - RasterStatus raster_status = RasterStatus::kFailed; - Pipeline::Consumer consumer = - [&](std::unique_ptr layer_tree) { + LayerTreePipeline::Consumer consumer = + [&](std::unique_ptr item) { + std::unique_ptr layer_tree = std::move(item->layer_tree); + std::unique_ptr frame_timings_recorder = + std::move(item->frame_timings_recorder); if (discard_callback(*layer_tree.get())) { raster_status = RasterStatus::kDiscarded; } else { @@ -199,9 +195,11 @@ RasterStatus Rasterizer::Draw( bool should_resubmit_frame = ShouldResubmitFrame(raster_status); if (should_resubmit_frame) { + auto resubmitted_layer_tree_item = std::make_unique( + std::move(resubmitted_layer_tree_), std::move(resubmitted_recorder_)); auto front_continuation = pipeline->ProduceIfEmpty(); PipelineProduceResult result = - front_continuation.Complete(std::move(resubmitted_layer_tree_)); + front_continuation.Complete(std::move(resubmitted_layer_tree_item)); if (result.success) { consume_result = PipelineConsumeResult::MoreAvailable; } @@ -224,11 +222,9 @@ RasterStatus Rasterizer::Draw( delegate_.GetTaskRunners().GetRasterTaskRunner()->PostTask( fml::MakeCopyable( [weak_this = weak_factory_.GetWeakPtr(), pipeline, - resubmit_recorder = std::move(resubmit_recorder), discard_callback = std::move(discard_callback)]() mutable { if (weak_this) { - weak_this->Draw(std::move(resubmit_recorder), pipeline, - std::move(discard_callback)); + weak_this->Draw(pipeline, std::move(discard_callback)); } })); break; @@ -395,6 +391,8 @@ fml::Milliseconds Rasterizer::GetFrameBudget() const { RasterStatus Rasterizer::DoDraw( std::unique_ptr frame_timings_recorder, std::unique_ptr layer_tree) { + TRACE_EVENT_WITH_FRAME_NUMBER(frame_timings_recorder, "flutter", + "Rasterizer::DoDraw"); FML_DCHECK(delegate_.GetTaskRunners() .GetRasterTaskRunner() ->RunsTasksOnCurrentThread()); @@ -412,6 +410,8 @@ RasterStatus Rasterizer::DoDraw( last_layer_tree_ = std::move(layer_tree); } else if (ShouldResubmitFrame(raster_status)) { resubmitted_layer_tree_ = std::move(layer_tree); + resubmitted_recorder_ = frame_timings_recorder->CloneUntil( + FrameTimingsRecorder::State::kBuildEnd); return raster_status; } else if (raster_status == RasterStatus::kDiscarded) { return raster_status; diff --git a/engine/src/flutter/shell/common/rasterizer.h b/engine/src/flutter/shell/common/rasterizer.h index bab92fc2fe..1979c1db3c 100644 --- a/engine/src/flutter/shell/common/rasterizer.h +++ b/engine/src/flutter/shell/common/rasterizer.h @@ -241,10 +241,8 @@ class Rasterizer final : public SnapshotDelegate, /// @param[in] discard_callback if specified and returns true, the layer tree /// is discarded instead of being rendered /// - RasterStatus Draw( - std::unique_ptr frame_timings_recorder, - std::shared_ptr> pipeline, - LayerTreeDiscardCallback discard_callback = NoDiscard); + RasterStatus Draw(std::shared_ptr pipeline, + LayerTreeDiscardCallback discard_callback = NoDiscard); //---------------------------------------------------------------------------- /// @brief The type of the screenshot to obtain of the previously @@ -509,6 +507,7 @@ class Rasterizer final : public SnapshotDelegate, // has not successfully rasterized. This can happen due to the change in the // thread configuration. This will be inserted to the front of the pipeline. std::unique_ptr resubmitted_layer_tree_; + std::unique_ptr resubmitted_recorder_; fml::closure next_frame_callback_; bool user_override_resource_cache_bytes_; std::optional max_cache_bytes_; diff --git a/engine/src/flutter/shell/common/rasterizer_unittests.cc b/engine/src/flutter/shell/common/rasterizer_unittests.cc index b626bd386b..b420d9ca4f 100644 --- a/engine/src/flutter/shell/common/rasterizer_unittests.cc +++ b/engine/src/flutter/shell/common/rasterizer_unittests.cc @@ -9,6 +9,7 @@ #include #include "flutter/flow/frame_timings.h" +#include "flutter/fml/synchronization/count_down_latch.h" #include "flutter/fml/time/time_point.h" #include "flutter/shell/common/thread_host.h" #include "flutter/testing/testing.h" @@ -80,16 +81,20 @@ TEST(RasterizerTest, create) { EXPECT_TRUE(rasterizer != nullptr); } -static std::unique_ptr CreateFinishedBuildRecorder() { +static std::unique_ptr CreateFinishedBuildRecorder( + fml::TimePoint timestamp) { std::unique_ptr recorder = std::make_unique(); - const auto now = fml::TimePoint::Now(); - recorder->RecordVsync(now, now); - recorder->RecordBuildStart(now); - recorder->RecordBuildEnd(now); + recorder->RecordVsync(timestamp, timestamp); + recorder->RecordBuildStart(timestamp); + recorder->RecordBuildEnd(timestamp); return recorder; } +static std::unique_ptr CreateFinishedBuildRecorder() { + return CreateFinishedBuildRecorder(fml::TimePoint::Now()); +} + TEST(RasterizerTest, drawEmptyPipeline) { std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); @@ -109,8 +114,8 @@ TEST(RasterizerTest, drawEmptyPipeline) { rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, nullptr); + auto pipeline = std::make_shared(/*depth=*/10); + rasterizer->Draw(pipeline, nullptr); latch.Signal(); }); latch.Wait(); @@ -167,14 +172,16 @@ TEST(RasterizerTest, rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + rasterizer->Draw(pipeline, no_discard); latch.Signal(); }); latch.Wait(); @@ -227,14 +234,16 @@ TEST( rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + rasterizer->Draw(pipeline, no_discard); latch.Signal(); }); latch.Wait(); @@ -293,14 +302,16 @@ TEST( rasterizer->Setup(std::move(surface)); - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + rasterizer->Draw(pipeline, no_discard); } TEST(RasterizerTest, @@ -361,17 +372,19 @@ TEST(RasterizerTest, rasterizer->Setup(std::move(surface)); - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; // The Draw() will respectively call BeginFrame(), SubmitFrame() and // EndFrame() one time. - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + rasterizer->Draw(pipeline, no_discard); // The DrawLastLayerTree() will respectively call BeginFrame(), SubmitFrame() // and EndFrame() one more time, totally 2 times. @@ -406,14 +419,16 @@ TEST(RasterizerTest, externalViewEmbedderDoesntEndFrameWhenNoSurfaceIsSet) { fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + rasterizer->Draw(pipeline, no_discard); latch.Signal(); }); latch.Wait(); @@ -457,16 +472,17 @@ TEST(RasterizerTest, externalViewEmbedderDoesntEndFrameWhenNotUsedThisFrame) { fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); // Always discard the layer tree. auto discard_callback = [](LayerTree&) { return true; }; - RasterStatus status = rasterizer->Draw(CreateFinishedBuildRecorder(), - pipeline, discard_callback); + RasterStatus status = rasterizer->Draw(pipeline, discard_callback); EXPECT_EQ(status, RasterStatus::kDiscarded); latch.Signal(); }); @@ -506,10 +522,9 @@ TEST(RasterizerTest, externalViewEmbedderDoesntEndFrameWhenPipelineIsEmpty) { fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto no_discard = [](LayerTree&) { return false; }; - RasterStatus status = - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + RasterStatus status = rasterizer->Draw(pipeline, no_discard); EXPECT_EQ(status, RasterStatus::kFailed); latch.Signal(); }); @@ -554,14 +569,16 @@ TEST(RasterizerTest, rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + rasterizer->Draw(pipeline, no_discard); latch.Signal(); }); latch.Wait(); @@ -606,15 +623,16 @@ TEST( rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - RasterStatus status = - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + RasterStatus status = rasterizer->Draw(pipeline, no_discard); EXPECT_EQ(status, RasterStatus::kSuccess); latch.Signal(); }); @@ -659,15 +677,16 @@ TEST( rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - RasterStatus status = - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + RasterStatus status = rasterizer->Draw(pipeline, no_discard); EXPECT_EQ(status, RasterStatus::kSuccess); latch.Signal(); }); @@ -711,19 +730,104 @@ TEST( rasterizer->Setup(std::move(surface)); fml::AutoResetWaitableEvent latch; thread_host.raster_thread->GetTaskRunner()->PostTask([&] { - auto pipeline = std::make_shared>(/*depth=*/10); + auto pipeline = std::make_shared(/*depth=*/10); auto layer_tree = std::make_unique(/*frame_size=*/SkISize(), /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder()); PipelineProduceResult result = - pipeline->Produce().Complete(std::move(layer_tree)); + pipeline->Produce().Complete(std::move(layer_tree_item)); EXPECT_TRUE(result.success); auto no_discard = [](LayerTree&) { return false; }; - RasterStatus status = - rasterizer->Draw(CreateFinishedBuildRecorder(), pipeline, no_discard); + RasterStatus status = rasterizer->Draw(pipeline, no_discard); EXPECT_EQ(status, RasterStatus::kDiscarded); latch.Signal(); }); latch.Wait(); } +TEST(RasterizerTest, + drawLayerTreeWithCorrectFrameTimingWhenPipelineIsMoreAvailable) { + std::string test_name = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + ThreadHost thread_host("io.flutter.test." + test_name + ".", + ThreadHost::Type::Platform | ThreadHost::Type::RASTER | + ThreadHost::Type::IO | ThreadHost::Type::UI); + TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), + thread_host.raster_thread->GetTaskRunner(), + thread_host.ui_thread->GetTaskRunner(), + thread_host.io_thread->GetTaskRunner()); + MockDelegate delegate; + ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners)); + + fml::AutoResetWaitableEvent latch; + std::unique_ptr rasterizer; + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { + rasterizer = std::make_unique(delegate); + latch.Signal(); + }); + latch.Wait(); + + auto surface = std::make_unique(); + EXPECT_CALL(*surface, AllowsDrawingWhenGpuDisabled()) + .WillRepeatedly(Return(true)); + ON_CALL(*surface, AcquireFrame(SkISize())) + .WillByDefault(::testing::Invoke([] { + SurfaceFrame::FramebufferInfo framebuffer_info; + framebuffer_info.supports_readback = true; + return std::make_unique( + /*surface=*/nullptr, framebuffer_info, + /*submit_callback=*/[](const SurfaceFrame&, SkCanvas*) { + return true; + }); + })); + ON_CALL(*surface, MakeRenderContextCurrent()) + .WillByDefault(::testing::Invoke( + [] { return std::make_unique(true); })); + + fml::CountDownLatch count_down_latch(2); + auto first_timestamp = fml::TimePoint::Now(); + auto second_timestamp = first_timestamp + fml::TimeDelta::FromMilliseconds(8); + std::vector timestamps = {first_timestamp, second_timestamp}; + int frame_rasterized_count = 0; + EXPECT_CALL(delegate, OnFrameRasterized(_)) + .Times(2) + .WillRepeatedly([&](const FrameTiming& frame_timing) { + EXPECT_EQ(timestamps[frame_rasterized_count], + frame_timing.Get(FrameTiming::kVsyncStart)); + EXPECT_EQ(timestamps[frame_rasterized_count], + frame_timing.Get(FrameTiming::kBuildStart)); + EXPECT_EQ(timestamps[frame_rasterized_count], + frame_timing.Get(FrameTiming::kBuildFinish)); + frame_rasterized_count++; + count_down_latch.CountDown(); + }); + + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { + rasterizer->Setup(std::move(surface)); + auto pipeline = std::make_shared(/*depth=*/10); + for (int i = 0; i < 2; i++) { + auto layer_tree = + std::make_unique(/*frame_size=*/SkISize(), + /*device_pixel_ratio=*/2.0f); + auto layer_tree_item = std::make_unique( + std::move(layer_tree), CreateFinishedBuildRecorder(timestamps[i])); + PipelineProduceResult result = + pipeline->Produce().Complete(std::move(layer_tree_item)); + EXPECT_TRUE(result.success); + EXPECT_EQ(result.is_first_item, i == 0); + } + auto no_discard = [](LayerTree&) { return false; }; + // Although we only call 'Rasterizer::Draw' once, it will be called twice + // finally because there are two items in the pipeline. + rasterizer->Draw(pipeline, no_discard); + }); + count_down_latch.Wait(); + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { + rasterizer.reset(); + latch.Signal(); + }); + latch.Wait(); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 7f870b7b24..05c608fd96 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -1150,9 +1150,7 @@ void Shell::OnAnimatorUpdateLatestFrameTargetTime( } // |Animator::Delegate| -void Shell::OnAnimatorDraw( - std::shared_ptr> pipeline, - std::unique_ptr frame_timings_recorder) { +void Shell::OnAnimatorDraw(std::shared_ptr pipeline) { FML_DCHECK(is_setup_); auto discard_callback = [this](flutter::LayerTree& tree) { @@ -1165,14 +1163,12 @@ void Shell::OnAnimatorDraw( [&waiting_for_first_frame = waiting_for_first_frame_, &waiting_for_first_frame_condition = waiting_for_first_frame_condition_, rasterizer = rasterizer_->GetWeakPtr(), - weak_pipeline = std::weak_ptr>(pipeline), - discard_callback = std::move(discard_callback), - frame_timings_recorder = std::move(frame_timings_recorder)]() mutable { + weak_pipeline = std::weak_ptr(pipeline), + discard_callback = std::move(discard_callback)]() mutable { if (rasterizer) { - std::shared_ptr> pipeline = weak_pipeline.lock(); + std::shared_ptr pipeline = weak_pipeline.lock(); if (pipeline) { - rasterizer->Draw(std::move(frame_timings_recorder), - std::move(pipeline), std::move(discard_callback)); + rasterizer->Draw(std::move(pipeline), std::move(discard_callback)); } if (waiting_for_first_frame.load()) { diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index 2e7d048214..75761b9177 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -600,9 +600,7 @@ class Shell final : public PlatformView::Delegate, fml::TimePoint frame_target_time) override; // |Animator::Delegate| - void OnAnimatorDraw( - std::shared_ptr> pipeline, - std::unique_ptr frame_timings_recorder) override; + void OnAnimatorDraw(std::shared_ptr pipeline) override; // |Animator::Delegate| void OnAnimatorDrawLastLayerTree(