Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 99509a7

Browse files
authored
Correct FrameTimingRecorder's raster start time. (#38674)
* ++ * ++ * Add tests * Add missing trace * ++ * ++ * ++
1 parent 8aa26ba commit 99509a7

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

shell/common/rasterizer.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,16 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe(
504504
embedder_root_canvas = external_view_embedder_->GetRootCanvas();
505505
}
506506

507+
frame_timings_recorder.RecordRasterStart(fml::TimePoint::Now());
508+
507509
// On Android, the external view embedder deletes surfaces in `BeginFrame`.
508510
//
509511
// Deleting a surface also clears the GL context. Therefore, acquire the
510512
// frame after calling `BeginFrame` as this operation resets the GL context.
511513
auto frame = surface_->AcquireFrame(layer_tree.frame_size());
512514
if (frame == nullptr) {
515+
frame_timings_recorder.RecordRasterEnd(
516+
&compositor_context_->raster_cache());
513517
return RasterStatus::kFailed;
514518
}
515519

@@ -536,7 +540,6 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe(
536540
);
537541
if (compositor_frame) {
538542
compositor_context_->raster_cache().BeginFrame();
539-
frame_timings_recorder.RecordRasterStart(fml::TimePoint::Now());
540543

541544
std::unique_ptr<FrameDamage> damage;
542545
// when leaf layer tracing is enabled we wish to repaint the whole frame

shell/common/rasterizer_unittests.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,61 @@ TEST(
785785
latch.Wait();
786786
}
787787

788+
TEST(
789+
RasterizerTest,
790+
FrameTimingRecorderShouldStartRecordingRasterTimeBeforeSurfaceAcquireFrame) {
791+
std::string test_name =
792+
::testing::UnitTest::GetInstance()->current_test_info()->name();
793+
ThreadHost thread_host("io.flutter.test." + test_name + ".",
794+
ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
795+
ThreadHost::Type::IO | ThreadHost::Type::UI);
796+
TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
797+
thread_host.raster_thread->GetTaskRunner(),
798+
thread_host.ui_thread->GetTaskRunner(),
799+
thread_host.io_thread->GetTaskRunner());
800+
NiceMock<MockDelegate> delegate;
801+
Settings settings;
802+
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
803+
EXPECT_CALL(delegate, GetTaskRunners())
804+
.WillRepeatedly(ReturnRef(task_runners));
805+
EXPECT_CALL(delegate, OnFrameRasterized(_))
806+
.WillOnce([&](const FrameTiming& frame_timing) {
807+
fml::TimePoint now = fml::TimePoint::Now();
808+
fml::TimePoint raster_start =
809+
frame_timing.Get(FrameTiming::kRasterStart);
810+
EXPECT_TRUE(now - raster_start < fml::TimeDelta::FromSecondsF(1));
811+
});
812+
813+
auto rasterizer = std::make_unique<Rasterizer>(delegate);
814+
auto surface = std::make_unique<NiceMock<MockSurface>>();
815+
auto is_gpu_disabled_sync_switch =
816+
std::make_shared<const fml::SyncSwitch>(false);
817+
ON_CALL(delegate, GetIsGpuDisabledSyncSwitch())
818+
.WillByDefault(Return(is_gpu_disabled_sync_switch));
819+
ON_CALL(*surface, AcquireFrame(SkISize()))
820+
.WillByDefault(::testing::Invoke([] { return nullptr; }));
821+
EXPECT_CALL(*surface, AcquireFrame(SkISize()));
822+
EXPECT_CALL(*surface, MakeRenderContextCurrent())
823+
.WillOnce(Return(ByMove(std::make_unique<GLContextDefaultResult>(true))));
824+
rasterizer->Setup(std::move(surface));
825+
fml::AutoResetWaitableEvent latch;
826+
thread_host.raster_thread->GetTaskRunner()->PostTask([&] {
827+
auto pipeline = std::make_shared<LayerTreePipeline>(/*depth=*/10);
828+
auto layer_tree = std::make_shared<LayerTree>(/*frame_size=*/SkISize(),
829+
/*device_pixel_ratio=*/2.0f);
830+
auto layer_tree_item = std::make_unique<LayerTreeItem>(
831+
std::move(layer_tree), CreateFinishedBuildRecorder());
832+
PipelineProduceResult result =
833+
pipeline->Produce().Complete(std::move(layer_tree_item));
834+
EXPECT_TRUE(result.success);
835+
auto no_discard = [](LayerTree&) { return false; };
836+
RasterStatus status = rasterizer->Draw(pipeline, no_discard);
837+
EXPECT_EQ(status, RasterStatus::kFailed);
838+
latch.Signal();
839+
});
840+
latch.Wait();
841+
}
842+
788843
TEST(RasterizerTest,
789844
drawLayerTreeWithCorrectFrameTimingWhenPipelineIsMoreAvailable) {
790845
std::string test_name =

0 commit comments

Comments
 (0)