From 1f0d7cd7ea4f7180a9ad7c85fd2752f73cedc5c8 Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Mon, 27 Nov 2023 15:27:34 -0800 Subject: [PATCH] SurfaceFrame root DisplayLists will no longer prepare an RTree (flutter/engine#48422) The root slice of a surface frame was requesting the construction of an RTree from its DisplayList when it was built from a layer tree, but since the layer tree already does branch culling, it would not benefit from any further culling during `DisplayList::Dispatch`. Further, if there are platform views present in the layer tree then they will need an RTree to accurately convey "pixel ownership" information to the platform, but the root slice lives below any and all platform views, so it is the only slice that doesn't need an RTree for that case. The cost of having an RTree in that slice was the accumulation of information and lists of rects that would never prove useful. --- .../src/flutter/display_list/display_list.cc | 7 +--- engine/src/flutter/flow/embedded_views.cc | 1 + engine/src/flutter/flow/surface_frame.cc | 9 ++++- .../flutter/flow/surface_frame_unittests.cc | 38 ++++++++++++++----- .../lib/ui/painting/picture_recorder.cc | 6 ++- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/engine/src/flutter/display_list/display_list.cc b/engine/src/flutter/display_list/display_list.cc index e06bcf246d..dfacba0c11 100644 --- a/engine/src/flutter/display_list/display_list.cc +++ b/engine/src/flutter/display_list/display_list.cc @@ -153,17 +153,12 @@ void DisplayList::Dispatch(DlOpReceiver& receiver, if (cull_rect.isEmpty()) { return; } - if (cull_rect.contains(bounds())) { + if (!has_rtree() || cull_rect.contains(bounds())) { Dispatch(receiver); return; } const DlRTree* rtree = this->rtree().get(); FML_DCHECK(rtree != nullptr); - if (rtree == nullptr) { - FML_LOG(ERROR) << "dispatched with culling rect on DL with no rtree"; - Dispatch(receiver); - return; - } uint8_t* ptr = storage_.get(); std::vector rect_indices; rtree->search(cull_rect, &rect_indices); diff --git a/engine/src/flutter/flow/embedded_views.cc b/engine/src/flutter/flow/embedded_views.cc index 1bf6e6c1d2..e1936d1af7 100644 --- a/engine/src/flutter/flow/embedded_views.cc +++ b/engine/src/flutter/flow/embedded_views.cc @@ -18,6 +18,7 @@ DlCanvas* DisplayListEmbedderViewSlice::canvas() { void DisplayListEmbedderViewSlice::end_recording() { display_list_ = builder_->Build(); + FML_DCHECK(display_list_->has_rtree()); builder_ = nullptr; } diff --git a/engine/src/flutter/flow/surface_frame.cc b/engine/src/flutter/flow/surface_frame.cc index dd1b98d33b..7f7e8a8da8 100644 --- a/engine/src/flutter/flow/surface_frame.cc +++ b/engine/src/flutter/flow/surface_frame.cc @@ -31,8 +31,13 @@ SurfaceFrame::SurfaceFrame(sk_sp surface, canvas_ = &adapter_; } else if (display_list_fallback) { FML_DCHECK(!frame_size.isEmpty()); - dl_builder_ = - sk_make_sp(SkRect::Make(frame_size), true); + // The root frame of a surface will be filled by the layer_tree which + // performs branch culling so it will be unlikely to need an rtree for + // further culling during `DisplayList::Dispatch`. Further, this canvas + // will live underneath any platform views so we do not need to compute + // exact coverage to describe "pixel ownership" to the platform. + dl_builder_ = sk_make_sp(SkRect::Make(frame_size), + /*prepare_rtree=*/false); canvas_ = dl_builder_.get(); } } diff --git a/engine/src/flutter/flow/surface_frame_unittests.cc b/engine/src/flutter/flow/surface_frame_unittests.cc index 0f6f9ba308..81516ad266 100644 --- a/engine/src/flutter/flow/surface_frame_unittests.cc +++ b/engine/src/flutter/flow/surface_frame_unittests.cc @@ -11,27 +11,45 @@ namespace flutter { TEST(FlowTest, SurfaceFrameDoesNotSubmitInDtor) { SurfaceFrame::FramebufferInfo framebuffer_info; + auto callback = [](const SurfaceFrame&, DlCanvas*) { + EXPECT_FALSE(true); + return true; + }; auto surface_frame = std::make_unique( - /*surface=*/nullptr, framebuffer_info, - /*submit_callback=*/ - [](const SurfaceFrame&, DlCanvas*) { - EXPECT_FALSE(true); - return true; - }, - SkISize::Make(800, 600)); + /*surface=*/nullptr, + /*framebuffer_info=*/framebuffer_info, + /*submit_callback=*/callback, + /*frame_size=*/SkISize::Make(800, 600)); surface_frame.reset(); } TEST(FlowTest, SurfaceFrameDoesNotHaveEmptyCanvas) { SurfaceFrame::FramebufferInfo framebuffer_info; + auto callback = [](const SurfaceFrame&, DlCanvas*) { return true; }; SurfaceFrame frame( - /*surface=*/nullptr, framebuffer_info, - /*submit_callback=*/[](const SurfaceFrame&, DlCanvas*) { return true; }, + /*surface=*/nullptr, + /*framebuffer_info=*/framebuffer_info, + /*submit_callback=*/callback, /*frame_size=*/SkISize::Make(800, 600), - /*context_result=*/nullptr, /*display_list_fallback=*/true); + /*context_result=*/nullptr, + /*display_list_fallback=*/true); EXPECT_FALSE(frame.Canvas()->GetLocalClipBounds().isEmpty()); EXPECT_FALSE(frame.Canvas()->QuickReject(SkRect::MakeLTRB(10, 10, 50, 50))); } +TEST(FlowTest, SurfaceFrameDoesNotPrepareRtree) { + SurfaceFrame::FramebufferInfo framebuffer_info; + auto callback = [](const SurfaceFrame&, DlCanvas*) { return true; }; + auto surface_frame = std::make_unique( + /*surface=*/nullptr, + /*framebuffer_info=*/framebuffer_info, + /*submit_callback=*/callback, + /*frame_size=*/SkISize::Make(800, 600), + /*context_result=*/nullptr, + /*display_list_fallback=*/true); + surface_frame->Canvas()->DrawRect(SkRect::MakeWH(100, 100), DlPaint()); + EXPECT_FALSE(surface_frame->BuildDisplayList()->has_rtree()); +} + } // namespace flutter diff --git a/engine/src/flutter/lib/ui/painting/picture_recorder.cc b/engine/src/flutter/lib/ui/painting/picture_recorder.cc index 6a24e93469..58d9d86632 100644 --- a/engine/src/flutter/lib/ui/painting/picture_recorder.cc +++ b/engine/src/flutter/lib/ui/painting/picture_recorder.cc @@ -36,10 +36,12 @@ void PictureRecorder::endRecording(Dart_Handle dart_picture) { return; } - Picture::CreateAndAssociateWithDartWrapper(dart_picture, - display_list_builder_->Build()); + auto display_list = display_list_builder_->Build(); display_list_builder_ = nullptr; + FML_DCHECK(display_list->has_rtree()); + Picture::CreateAndAssociateWithDartWrapper(dart_picture, display_list); + canvas_->Invalidate(); canvas_ = nullptr; ClearDartWrapper();