From 7ba399328c71be45d69b86736a9d2131edab4e99 Mon Sep 17 00:00:00 2001 From: zhangbingce Date: Wed, 5 Jul 2023 15:00:11 +0800 Subject: [PATCH] =?UTF-8?q?[canvas]touch=E7=A7=BB=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E5=8D=87cpu=E9=A2=91=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangbingce Change-Id: I5a724dba0ee18119f8ae788f88a830538e2dd77b --- .../custom_paint/custom_paint_pattern.cpp | 56 +++++++++++++++++++ .../custom_paint/custom_paint_pattern.h | 8 ++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.cpp b/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.cpp index 62e828e42b2..b406c79ef79 100644 --- a/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.cpp +++ b/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.cpp @@ -17,12 +17,15 @@ #include "drawing/engine_adapter/skia_adapter/skia_canvas.h" +#include "base/ressched/ressched_report.h" +#include "base/utils/time_util.h" #include "base/utils/utils.h" #include "core/common/ace_application_info.h" #include "core/components_ng/pattern/custom_paint/canvas_paint_method.h" #include "core/components_ng/pattern/custom_paint/offscreen_canvas_pattern.h" #include "core/components_ng/pattern/custom_paint/rendering_context2d_modifier.h" #include "core/components_ng/render/adapter/rosen_render_context.h" +#include "core/event/touch_event.h" namespace { constexpr int32_t PLATFORM_VERSION_TEN = 10; @@ -30,6 +33,9 @@ constexpr int32_t PLATFORM_VERSION_TEN = 10; namespace OHOS::Ace::NG { class RosenRenderContext; +#ifdef OHOS_PLATFORM +constexpr int64_t INCREASE_CPU_TIME_ONCE = 4000000000; // 4s(unit: ns) +#endif void CustomPaintPattern::OnAttachToFrameNode() { auto host = GetHost(); @@ -44,6 +50,7 @@ void CustomPaintPattern::OnAttachToFrameNode() AceType::MakeRefPtr(); } paintMethod_ = MakeRefPtr(context, contentModifier_); + InitEvent(); } RefPtr CustomPaintPattern::CreateNodePaintMethod() @@ -79,6 +86,55 @@ bool CustomPaintPattern::OnDirtyLayoutWrapperSwap(const RefPtr& d return false; } +void CustomPaintPattern::InitEvent() +{ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto eventHub = host->GetEventHub(); + CHECK_NULL_VOID(eventHub); + auto gestureHub = eventHub->GetOrCreateGestureEventHub(); + CHECK_NULL_VOID(gestureHub); + InitTouchEvent(gestureHub); +} + +void CustomPaintPattern::InitTouchEvent(const RefPtr& gestureHub) +{ + CHECK_NULL_VOID_NOLOG(!touchEvent_); + + auto touchTask = [weak = WeakClaim(this)](const TouchEventInfo& info) { + auto pattern = weak.Upgrade(); + CHECK_NULL_VOID(pattern); + pattern->HandleTouchEvent(info); + }; + + touchEvent_ = MakeRefPtr(std::move(touchTask)); + gestureHub->AddTouchEvent(touchEvent_); +} + +void CustomPaintPattern::HandleTouchEvent(const TouchEventInfo& info) +{ +#ifdef OHOS_PLATFORM + auto touchInfoList = info.GetChangedTouches(); + if (touchInfoList.empty()) { + return; + } + auto touchType = touchInfoList.front().GetTouchType(); + // increase cpu frequency + if (touchType == TouchType::MOVE) { + auto currentTime = GetSysTimestamp(); + auto increaseCpuTime = currentTime - startIncreaseTime_; + if (increaseCpuTime >= INCREASE_CPU_TIME_ONCE) { + LOGD("HandleTouchEvent increase cpu frequency"); + startIncreaseTime_ = currentTime; + ResSchedReport::GetInstance().ResSchedDataReport("slide_on"); + } + } else if (touchType == TouchType::UP) { + startIncreaseTime_ = 0; + ResSchedReport::GetInstance().ResSchedDataReport("slide_off"); + } +#endif +} + void CustomPaintPattern::SetAntiAlias(bool isEnabled) { auto task = [isEnabled](CanvasPaintMethod& paintMethod, PaintWrapper* paintWrapper) { diff --git a/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.h b/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.h index 3e14ad2965b..2331aff071d 100644 --- a/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.h +++ b/frameworks/core/components_ng/pattern/custom_paint/custom_paint_pattern.h @@ -143,13 +143,19 @@ public: private: void OnAttachToFrameNode() override; bool OnDirtyLayoutWrapperSwap(const RefPtr& dirty, const DirtySwapConfig& config) override; + void InitEvent(); + void InitTouchEvent(const RefPtr& gestureHub); + void HandleTouchEvent(const TouchEventInfo& info); RefPtr paintMethod_; std::optional canvasSize_; bool isCanvasInit_ = false; RefPtr contentModifier_; - + RefPtr touchEvent_; +#ifdef OHOS_PLATFORM + int64_t startIncreaseTime_ = 0; +#endif ACE_DISALLOW_COPY_AND_MOVE(CustomPaintPattern); }; } // namespace OHOS::Ace::NG -- Gitee