From 59025657a256603c608ced413d01399b9167147d Mon Sep 17 00:00:00 2001 From: jiangdayuan Date: Mon, 25 Apr 2022 10:25:10 +0800 Subject: [PATCH] increase scroll cpu frequency when sliding Signed-off-by: jiangdayuan --- .../core/components/scroll/scrollable.cpp | 80 ++++++++++++------- .../core/components/scroll/scrollable.h | 4 + .../core/components/swiper/render_swiper.cpp | 8 +- 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/frameworks/core/components/scroll/scrollable.cpp b/frameworks/core/components/scroll/scrollable.cpp index 0e8ef5af..64292080 100644 --- a/frameworks/core/components/scroll/scrollable.cpp +++ b/frameworks/core/components/scroll/scrollable.cpp @@ -20,6 +20,7 @@ #include "base/log/ace_trace.h" #include "base/log/log.h" #include "base/ressched/ressched_report.h" +#include "base/utils/time_util.h" namespace OHOS::Ace { namespace { @@ -54,6 +55,10 @@ constexpr double ADJUSTABLE_VELOCITY = 0.0; #endif #endif +#ifdef OHOS_PLATFORM +constexpr int64_t INCREASE_CPU_TIME_ONCE = 4000000000; // 4s(unit: ns) +#endif + } // namespace // Static Functions. @@ -109,7 +114,6 @@ void Scrollable::Initialize(const WeakPtr& context) scrollEvent.eventType = "scrollstart"; context->SendEventToAccessibility(scrollEvent); } - scroll->moved_ = false; scroll->HandleDragStart(info); } }); @@ -168,9 +172,11 @@ void Scrollable::HandleTouchDown() LOGD("handle touch down"); isTouching_ = true; + if (outBoundaryCallback_ && !outBoundaryCallback_()) { + return; + } // If animation still runs, first stop it. springController_->Stop(); - if (!controller_->IsStopped()) { controller_->Stop(); if (motion_) { @@ -190,11 +196,12 @@ void Scrollable::HandleTouchUp() { LOGD("handle touch up"); isTouching_ = false; - if (springController_->IsStopped()) { - if (scrollOverCallback_ && outBoundaryCallback_ && outBoundaryCallback_()) { - LOGD("need scroll to boundary"); - ProcessScrollOverCallback(0.0); - } + if (outBoundaryCallback_ && !outBoundaryCallback_()) { + return; + } + if (springController_->IsStopped() && scrollOverCallback_) { + LOGD("need scroll to boundary"); + ProcessScrollOverCallback(0.0); } } @@ -210,8 +217,7 @@ bool Scrollable::Idle() const bool Scrollable::IsStopped() const { - return (springController_ ? (springController_->IsStopped()) : true) && - (controller_ ? (controller_->IsStopped()) : true); + return (!springController_ || (springController_->IsStopped())) && (!controller_ || (controller_->IsStopped())); } void Scrollable::StopScrollable() @@ -232,8 +238,14 @@ void Scrollable::HandleDragStart(const OHOS::Ace::DragStartInfo& info) LOGD("HandleDragStart. LocalLocation: %{public}s, GlobalLocation: %{public}s", info.GetLocalLocation().ToString().c_str(), info.GetGlobalLocation().ToString().c_str()); #ifdef OHOS_PLATFORM - // Increase the cpu frequency when sliding. - ResSchedReport::GetInstance().ResSchedDataReport("slide_on"); + // Increase the cpu frequency when sliding start. + auto currentTime = GetSysTimestamp(); + auto increaseCpuTime = currentTime - startIncreaseTime_; + if (moved_ == false || increaseCpuTime >= INCREASE_CPU_TIME_ONCE) { + LOGI("HandleDragStart increase cpu frequency, moved_ = %{public}d", moved_); + startIncreaseTime_ = currentTime; + ResSchedReport::GetInstance().ResSchedDataReport("slide_on"); + } #endif UpdateScrollPosition(dragPositionInMainAxis, SCROLL_FROM_START); RelatedEventStart(); @@ -248,10 +260,21 @@ void Scrollable::HandleDragUpdate(const DragUpdateInfo& info) ACE_FUNCTION_TRACE(); if (!springController_->IsStopped() || !controller_->IsStopped()) { // If animation still runs, first stop it. + isDragUpdateStop_ = true; controller_->Stop(); springController_->Stop(); currentPos_ = 0.0; } +#ifdef OHOS_PLATFORM + // Handle the case where you keep sliding past limit time(4s). + auto currentTime = GetSysTimestamp(); + auto increaseCpuTime = currentTime - startIncreaseTime_; + if (increaseCpuTime >= INCREASE_CPU_TIME_ONCE) { + LOGI("HandleDragUpdate increase cpu frequency, moved_ = %{public}d", moved_); + startIncreaseTime_ = currentTime; + ResSchedReport::GetInstance().ResSchedDataReport("slide_on"); + } +#endif LOGD("handle drag update, offset is %{public}lf", info.GetMainDelta()); if (RelatedScrollEventPrepare(Offset(0.0, info.GetMainDelta()))) { return; @@ -265,24 +288,19 @@ void Scrollable::HandleDragEnd(const DragEndInfo& info) { LOGD("handle drag end, position is %{public}lf and %{public}lf, velocity is %{public}lf", info.GetGlobalLocation().GetX(), info.GetGlobalLocation().GetY(), info.GetMainVelocity()); - controller_->Stop(); - springController_->Stop(); controller_->ClearAllListeners(); springController_->ClearAllListeners(); + isDragUpdateStop_ = false; touchUp_ = false; scrollPause_ = false; - double correctVelocity = std::clamp(info.GetMainVelocity(), MIN_VELOCITY + slipFactor_, - MAX_VELOCITY - slipFactor_); + double correctVelocity = std::clamp(info.GetMainVelocity(), MIN_VELOCITY + slipFactor_, MAX_VELOCITY - slipFactor_); correctVelocity = correctVelocity * sVelocityScale_; currentVelocity_ = correctVelocity; -#ifdef OHOS_PLATFORM - ResSchedReport::GetInstance().ResSchedDataReport("slide_off"); -#endif if (dragEndCallback_) { dragEndCallback_(); } RelatedEventEnd(); - bool isOutBoundary = outBoundaryCallback_ ? outBoundaryCallback_() : false; + bool isOutBoundary = outBoundaryCallback_ && outBoundaryCallback_(); if (isOutBoundary && scrollOverCallback_) { ProcessScrollOverCallback(correctVelocity); } else { @@ -308,15 +326,7 @@ void Scrollable::HandleDragEnd(const DragEndInfo& info) currentVelocity_ = 0.0; // Starts motion. - controller_->ClearStartListeners(); controller_->ClearStopListeners(); - controller_->AddStartListener([weak = AceType::WeakClaim(this)]() { - auto scroll = weak.Upgrade(); - if (scroll) { - scroll->moved_ = true; - } - }); - controller_->PlayMotion(motion_); controller_->AddStopListener([weak = AceType::WeakClaim(this)]() { auto scroll = weak.Upgrade(); if (scroll) { @@ -331,6 +341,8 @@ void Scrollable::HandleDragEnd(const DragEndInfo& info) } } }); + controller_->PlayMotion(motion_); + moved_ = true; } } @@ -383,7 +395,14 @@ void Scrollable::StartSpringMotion( scroll->scrollEndCallback_(); } scroll->currentVelocity_ = 0.0; + if (scroll->isTouching_ || scroll->isDragUpdateStop_) { + return; + } scroll->moved_ = false; +#ifdef OHOS_PLATFORM + LOGI("springController stop increase cpu frequency"); + ResSchedReport::GetInstance().ResSchedDataReport("slide_off"); +#endif if (scroll->scrollEnd_) { scroll->scrollEnd_(); } @@ -411,7 +430,14 @@ void Scrollable::ProcessScrollMotionStop() ProcessScrollOverCallback(currentVelocity_); } else { currentVelocity_ = 0.0; + if (isDragUpdateStop_ || isTouching_) { + return; + } moved_ = false; +#ifdef OHOS_PLATFORM + LOGI("controller stop increase cpu frequency"); + ResSchedReport::GetInstance().ResSchedDataReport("slide_off"); +#endif if (scrollEnd_) { scrollEnd_(); } diff --git a/frameworks/core/components/scroll/scrollable.h b/frameworks/core/components/scroll/scrollable.h index 41846619..c0675c38 100644 --- a/frameworks/core/components/scroll/scrollable.h +++ b/frameworks/core/components/scroll/scrollable.h @@ -281,10 +281,14 @@ private: bool isTouching_ = false; bool available_ = true; bool needCenterFix_ = false; + bool isDragUpdateStop_ = false; int32_t nodeId_ = 0; double slipFactor_ = 0.0; static double sFriction_; static double sVelocityScale_; +#ifdef OHOS_PLATFORM + int64_t startIncreaseTime_ = 0; +#endif }; } // namespace OHOS::Ace diff --git a/frameworks/core/components/swiper/render_swiper.cpp b/frameworks/core/components/swiper/render_swiper.cpp index 36652dce..b4726291 100644 --- a/frameworks/core/components/swiper/render_swiper.cpp +++ b/frameworks/core/components/swiper/render_swiper.cpp @@ -308,17 +308,17 @@ void RenderSwiper::PerformLayout() LayoutParam innerLayout = GetLayoutParam(); Size minSize = GetLayoutParam().GetMinSize(); Size maxSize = GetLayoutParam().GetMaxSize(); - Size maxSizeClild = maxSize; + Size maxSizeChild = maxSize; auto showingCount = swiper_ ? swiper_->GetDisplayCount() : 1; double intervalSpace = swiper_ ? NormalizeToPx(swiper_->GetItemSpace()) : 0.0; if (axis_ == Axis::HORIZONTAL) { - maxSizeClild.SetWidth( + maxSizeChild.SetWidth( (maxSize.Width() - intervalSpace * (showingCount - 1)) / showingCount - prevMargin_ - nextMargin_); } else { - maxSizeClild.SetHeight( + maxSizeChild.SetHeight( (maxSize.Height() - intervalSpace * (showingCount - 1)) / showingCount - prevMargin_ - nextMargin_); } - innerLayout.SetMaxSize(maxSizeClild); + innerLayout.SetMaxSize(maxSizeChild); bool isLinearLayout = swiper_ ? swiper_->GetDisplayMode() == SwiperDisplayMode::AUTO_LINEAR : false; double maxWidth = minSize.Width();