From 50968b032438bf89348fa6a65f8821ae832c955e Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Thu, 1 Feb 2024 13:31:26 -0800 Subject: [PATCH] Get bounds from RTree in DLBuilder::Build() (flutter/engine#50253) This optimization was discovered when reviewing the bounds accumulation code in DisplayListBuilder. There is trivially wasted work in the `Build()` method that can be easily avoided. When a Builder is preparing an RTree, it accumulates a list of bounds for every rendering op, but doesn't union them up front. If you ask the Builder for the bounds of the accumulated ops, it will have to union all of the rectangles to produce an answer. But, if you ask the Builder to produce an RTree, that process will implicitly combine all of the rectangles as a side effect of creating the tree structure - and that RTree object has a bounds method that can return the result directly. This change leverages the construction of the RTree to avoid having to union all of the rects twice which can only save time when constructing the majority of DisplayList objects used in a scene. --- engine/src/flutter/display_list/dl_builder.cc | 7 +++++-- engine/src/flutter/display_list/dl_op_records.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/display_list/dl_builder.cc b/engine/src/flutter/display_list/dl_builder.cc index dc69687293..e6ba24ba7f 100644 --- a/engine/src/flutter/display_list/dl_builder.cc +++ b/engine/src/flutter/display_list/dl_builder.cc @@ -76,6 +76,9 @@ sk_sp DisplayListBuilder::Build() { bool is_safe = is_ui_thread_safe_; bool affects_transparency = current_layer_->affects_transparent_layer(); + sk_sp rtree = this->rtree(); + SkRect bounds = rtree ? rtree->bounds() : this->bounds(); + used_ = allocated_ = render_op_count_ = op_index_ = 0; nested_bytes_ = nested_op_count_ = 0; is_ui_thread_safe_ = true; @@ -86,8 +89,8 @@ sk_sp DisplayListBuilder::Build() { current_ = DlPaint(); return sk_sp(new DisplayList( - std::move(storage_), bytes, count, nested_bytes, nested_count, bounds(), - compatible, is_safe, affects_transparency, rtree())); + std::move(storage_), bytes, count, nested_bytes, nested_count, bounds, + compatible, is_safe, affects_transparency, std::move(rtree))); } DisplayListBuilder::DisplayListBuilder(const SkRect& cull_rect, diff --git a/engine/src/flutter/display_list/dl_op_records.h b/engine/src/flutter/display_list/dl_op_records.h index 259102c142..d0f12caea0 100644 --- a/engine/src/flutter/display_list/dl_op_records.h +++ b/engine/src/flutter/display_list/dl_op_records.h @@ -277,7 +277,7 @@ struct SetRuntimeEffectColorSourceOp : DLOp { struct SetSceneColorSourceOp : DLOp { static const auto kType = DisplayListOpType::kSetSceneColorSource; - SetSceneColorSourceOp(const DlSceneColorSource* source) + explicit SetSceneColorSourceOp(const DlSceneColorSource* source) : source(source->scene_node(), source->camera_matrix()) {} const DlSceneColorSource source;