Skip to content

Commit

Permalink
CPU impact improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 21, 2024
1 parent 7c95475 commit de76b48
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions Submodules/UIKit/include/CALayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class CALayer: public enable_shared_from_this<CALayer> {

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.
Expand Down
7 changes: 7 additions & 0 deletions Submodules/UIKit/lib/UIApplicationMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions Submodules/UIKit/lib/UILabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
18 changes: 14 additions & 4 deletions Submodules/UIKit/lib/UIView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace NXKit;

UIView::UIView(NXRect frame, std::shared_ptr<CALayer> layer) {
_yoga = new_shared<YGLayout>(shared_from_this());
// yoga->setEnabled(true);
// _yoga->setEnabled(true);

_layer = layer;
_layer->setAnchorPoint(NXPoint::zero);
Expand All @@ -25,6 +25,7 @@ std::shared_ptr<UIResponder> UIView::next() {

void UIView::setFrame(NXRect frame) {
if (this->frame().size != frame.size) {
setNeedsDisplay();
setNeedsLayout();
}
_layer->setFrame(frame);
Expand All @@ -33,6 +34,7 @@ void UIView::setFrame(NXRect frame) {

void UIView::setBounds(NXRect bounds) {
if (this->bounds().size != bounds.size) {
setNeedsDisplay();
setNeedsLayout();
// setNeedsUpdateSafeAreaInsets();
}
Expand Down Expand Up @@ -163,6 +165,7 @@ void UIView::drawAndLayoutTreeIfNeeded() {
// updateSafeAreaInsetsIfNeeded();
// updateLayoutMarginIfNeeded();
layoutIfNeeded();
_yoga->layoutIfNeeded();

for (auto& subview: _subviews) {
subview->drawAndLayoutTreeIfNeeded();
Expand Down Expand Up @@ -378,14 +381,18 @@ void UIView::traitCollectionDidChange(std::shared_ptr<UITraitCollection> previou
// MARK: - Layout
std::shared_ptr<UIView> 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();
Expand All @@ -396,6 +403,9 @@ void UIView::setNeedsLayout() {
void UIView::layoutIfNeeded() {
if (_needsLayout) {
_needsLayout = false;

layoutRoot()->layoutIfNeeded();

for (const auto &view : subviews()) {
view->setNeedsLayout();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<YGLayout> layout) {
layout->setFlexDirection(toggle ? YGFlexDirection::YGFlexDirectionColumnReverse : YGFlexDirection::YGFlexDirectionColumn);
});
Expand Down Expand Up @@ -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<UIBlurView>();
Expand Down

0 comments on commit de76b48

Please sign in to comment.