From de76b48a05049b7e988645c1a9772a1afc1c0457 Mon Sep 17 00:00:00 2001 From: Daniil Vinogradov Date: Sat, 21 Dec 2024 14:56:46 +0100 Subject: [PATCH] CPU impact improvements --- Submodules/UIKit/include/CALayer.h | 1 + Submodules/UIKit/lib/UIApplicationMain.cpp | 7 +++++++ Submodules/UIKit/lib/UILabel.cpp | 5 +++++ Submodules/UIKit/lib/UIView.cpp | 18 ++++++++++++++---- .../YogaTestViewController.cpp | 3 ++- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Submodules/UIKit/include/CALayer.h b/Submodules/UIKit/include/CALayer.h index c4cce40..f46dbf3 100644 --- a/Submodules/UIKit/include/CALayer.h +++ b/Submodules/UIKit/include/CALayer.h @@ -125,6 +125,7 @@ class CALayer: public enable_shared_from_this { private: friend class UIView; + friend bool applicationRunLoop(); /// Defaults to 1.0 but if the layer is associated with a view, /// the view sets this value to match the screen. diff --git a/Submodules/UIKit/lib/UIApplicationMain.cpp b/Submodules/UIKit/lib/UIApplicationMain.cpp index e530b38..bf66929 100644 --- a/Submodules/UIKit/lib/UIApplicationMain.cpp +++ b/Submodules/UIKit/lib/UIApplicationMain.cpp @@ -34,6 +34,13 @@ bool applicationRunLoop() { UIView::animateIfNeeded(currentTime); keyWindow->drawAndLayoutTreeIfNeeded(); + if (!CALayer::layerTreeIsDirty) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + return true; + } + + CALayer::layerTreeIsDirty = false; + auto surface = SkiaCtx::_main->getBackbufferSurface(); if (!surface) return true; diff --git a/Submodules/UIKit/lib/UILabel.cpp b/Submodules/UIKit/lib/UILabel.cpp index fa5c577..8ea4113 100644 --- a/Submodules/UIKit/lib/UILabel.cpp +++ b/Submodules/UIKit/lib/UILabel.cpp @@ -12,30 +12,35 @@ UILabel::UILabel(): UIView() { void UILabel::setText(std::string text) { if (_text == text) return; _text = text; + setNeedsDisplay(); setNeedsLayout(); } void UILabel::setTextColor(UIColor textColor) { if (_textColor == textColor) return; _textColor = textColor; + setNeedsDisplay(); setNeedsLayout(); } void UILabel::setFontSize(NXFloat fontSize) { if (_fontSize == fontSize) return; _fontSize = fontSize; + setNeedsDisplay(); setNeedsLayout(); } void UILabel::setTextAlignment(NSTextAlignment textAlignment) { if (_textAlignment == textAlignment) return; _textAlignment = textAlignment; + setNeedsDisplay(); setNeedsLayout(); } void UILabel::setFontWeight(NXFloat fontWeight) { if (_fontWeight == fontWeight) return; _fontWeight = fontWeight; + setNeedsDisplay(); setNeedsLayout(); } diff --git a/Submodules/UIKit/lib/UIView.cpp b/Submodules/UIKit/lib/UIView.cpp index 7b7692b..374cd0e 100644 --- a/Submodules/UIKit/lib/UIView.cpp +++ b/Submodules/UIKit/lib/UIView.cpp @@ -9,7 +9,7 @@ using namespace NXKit; UIView::UIView(NXRect frame, std::shared_ptr layer) { _yoga = new_shared(shared_from_this()); -// yoga->setEnabled(true); +// _yoga->setEnabled(true); _layer = layer; _layer->setAnchorPoint(NXPoint::zero); @@ -25,6 +25,7 @@ std::shared_ptr UIView::next() { void UIView::setFrame(NXRect frame) { if (this->frame().size != frame.size) { + setNeedsDisplay(); setNeedsLayout(); } _layer->setFrame(frame); @@ -33,6 +34,7 @@ void UIView::setFrame(NXRect frame) { void UIView::setBounds(NXRect bounds) { if (this->bounds().size != bounds.size) { + setNeedsDisplay(); setNeedsLayout(); // setNeedsUpdateSafeAreaInsets(); } @@ -163,6 +165,7 @@ void UIView::drawAndLayoutTreeIfNeeded() { // updateSafeAreaInsetsIfNeeded(); // updateLayoutMarginIfNeeded(); layoutIfNeeded(); + _yoga->layoutIfNeeded(); for (auto& subview: _subviews) { subview->drawAndLayoutTreeIfNeeded(); @@ -378,14 +381,18 @@ void UIView::traitCollectionDidChange(std::shared_ptr previou // MARK: - Layout std::shared_ptr UIView::layoutRoot() { auto view = shared_from_this(); - while (view && !view->_yoga->isRoot()) { + while (true) { + if (view->_yoga->isRoot() || view->superview().expired()) return view; view = view->superview().lock(); } - return view; +// while (view && !view->_yoga->isRoot()) { +// view = view->superview().lock(); +// } +// return view; } void UIView::setNeedsLayout() { - setNeedsDisplay(); +// setNeedsDisplay(); // Needs to recalculate Yoga from root auto layoutRoot = this->layoutRoot(); @@ -396,6 +403,9 @@ void UIView::setNeedsLayout() { void UIView::layoutIfNeeded() { if (_needsLayout) { _needsLayout = false; + + layoutRoot()->layoutIfNeeded(); + for (const auto &view : subviews()) { view->setNeedsLayout(); } diff --git a/app/Screens/YogaTestViewController/YogaTestViewController.cpp b/app/Screens/YogaTestViewController/YogaTestViewController.cpp index 02ae97e..c153801 100644 --- a/app/Screens/YogaTestViewController/YogaTestViewController.cpp +++ b/app/Screens/YogaTestViewController/YogaTestViewController.cpp @@ -52,7 +52,7 @@ void YogaTestViewController::loadView() { static bool toggle = false; toggle = !toggle; - UIView::animate(2, [view]() { + UIView::animate(0.3, 0, UIViewAnimationOptions::beginFromCurrentState, [view]() { view->configureLayout([](std::shared_ptr layout) { layout->setFlexDirection(toggle ? YGFlexDirection::YGFlexDirectionColumnReverse : YGFlexDirection::YGFlexDirectionColumn); }); @@ -87,6 +87,7 @@ void YogaTestViewController::loadView() { dragMeViewLabel->setFontWeight(600); dragMeViewLabel->setTextAlignment(NSTextAlignment::center); dragMeViewLabel->setAutolayoutEnabled(true); +// dragMeViewLabel->setHidden(true); // dragMeViewLabel->setFrame({ 80, 110, 44, 100 }); auto blur = new_shared();