From a4f009c56331940dca289e1f30029c0f4acf5f2d Mon Sep 17 00:00:00 2001 From: Tianer Zhou Date: Tue, 19 Nov 2024 11:36:16 +0800 Subject: [PATCH] optimize cache layout Signed-off-by: Tianer Zhou Change-Id: I6f9f4e696fab2a573bf810f8c7acbb22904725ad --- .../core/components_ng/layout/layout_wrapper.h | 3 +++ .../sliding_window/water_flow_layout_sw.cpp | 12 ++++++++++-- .../top_down/water_flow_layout_algorithm.cpp | 17 +++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/frameworks/core/components_ng/layout/layout_wrapper.h b/frameworks/core/components_ng/layout/layout_wrapper.h index a9184175bd4..f1248dbdd85 100644 --- a/frameworks/core/components_ng/layout/layout_wrapper.h +++ b/frameworks/core/components_ng/layout/layout_wrapper.h @@ -145,6 +145,9 @@ public: virtual RefPtr GetOrCreateChildByIndex( uint32_t index, bool addToRenderTree = true, bool isCache = false) = 0; + /** + * @param isCache if false, child is added to render tree and AttachToMainTree is called. + */ virtual RefPtr GetChildByIndex(uint32_t index, bool isCache = false) = 0; virtual ChildrenListWithGuard GetAllChildrenWithBuild(bool addToRenderTree = true) = 0; virtual void RemoveChildInRenderTree(uint32_t index) = 0; diff --git a/frameworks/core/components_ng/pattern/waterflow/layout/sliding_window/water_flow_layout_sw.cpp b/frameworks/core/components_ng/pattern/waterflow/layout/sliding_window/water_flow_layout_sw.cpp index 5b3f3faf28f..def1246daf9 100644 --- a/frameworks/core/components_ng/pattern/waterflow/layout/sliding_window/water_flow_layout_sw.cpp +++ b/frameworks/core/components_ng/pattern/waterflow/layout/sliding_window/water_flow_layout_sw.cpp @@ -671,6 +671,10 @@ float WaterFlowLayoutSW::MeasureChild(int32_t idx, size_t lane) const } child->Measure(WaterFlowLayoutUtils::CreateChildConstraint( { itemsCrossSize_[info_->GetSegment(idx)][lane], mainLen_, axis_ }, props_, child)); + if (cacheDeadline_) { + child->Layout(); + child->SetActive(false); + } const float res = child->GetGeometryNode()->GetMarginFrameSize().MainSize(info_->axis_); info_->CacheItemHeight(idx, res); return res; @@ -699,7 +703,8 @@ void WaterFlowLayoutSW::LayoutSection( const auto& lane = info_->lanes_[idx][i]; float mainPos = lane.startPos; for (const auto& item : lane.items_) { - const bool isCache = item.idx < info_->startIndex_ || item.idx > info_->endIndex_; + const bool isCache = !props_->GetShowCachedItemsValue(false) && + (item.idx < info_->startIndex_ || item.idx > info_->endIndex_); auto child = wrapper_->GetChildByIndex(nodeIdx(item.idx), isCache); if (!child) { mainPos += item.mainSize + mainGaps_[idx]; @@ -712,12 +717,15 @@ void WaterFlowLayoutSW::LayoutSection( } childNode->SetMarginFrameOffset(offset + paddingOffset); + mainPos += item.mainSize + mainGaps_[idx]; + if (isCache) { + continue; + } if (child->CheckNeedForceMeasureAndLayout()) { child->Layout(); } else { child->GetHostNode()->ForceSyncGeometryNode(); } - mainPos += item.mainSize + mainGaps_[idx]; } if (!rtl) { crossPos += itemsCrossSize_[idx][i] + crossGaps_[idx]; diff --git a/frameworks/core/components_ng/pattern/waterflow/layout/top_down/water_flow_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/waterflow/layout/top_down/water_flow_layout_algorithm.cpp index 5037bf3ec25..f4b13112774 100644 --- a/frameworks/core/components_ng/pattern/waterflow/layout/top_down/water_flow_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/waterflow/layout/top_down/water_flow_layout_algorithm.cpp @@ -243,19 +243,28 @@ void WaterFlowLayoutAlgorithm::Layout(LayoutWrapper* layoutWrapper) } else { currentOffset += OffsetF(mainOffset, crossOffset); } - const bool isCache = item.first < layoutInfo_->startIndex_ || item.first > layoutInfo_->endIndex_; - auto wrapper = layoutWrapper->GetChildByIndex(GetChildIndexWithFooter(item.first), isCache && !showCache); + const bool isCache = + !showCache && (item.first < layoutInfo_->startIndex_ || item.first > layoutInfo_->endIndex_); + auto wrapper = layoutWrapper->GetChildByIndex(GetChildIndexWithFooter(item.first), isCache); if (!wrapper) { continue; } wrapper->GetGeometryNode()->SetMarginFrameOffset(currentOffset); - wrapper->Layout(); + + if (isCache) { + continue; + } + if (wrapper->CheckNeedForceMeasureAndLayout()) { + wrapper->Layout(); + } else { + wrapper->GetHostNode()->ForceSyncGeometryNode(); + } // recode restore info if (item.first == layoutInfo_->startIndex_) { layoutInfo_->storedOffset_ = mainOffset; } - if (!isCache && NonNegative(mainOffset + item.second.second)) { + if (NonNegative(mainOffset + item.second.second)) { firstIndex = std::min(firstIndex, item.first); } auto frameNode = AceType::DynamicCast(wrapper);