1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "knuckle_glow_trace_system.h"
17
18 #include "include/core/SkPathMeasure.h"
19 #include "mmi_log.h"
20
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "KnuckleGlowTraceSystem"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr float BASIC_DISTANCE_BETWEEN_POINTS { 5.0f };
28 } // namespace
29
KnuckleGlowTraceSystem(int32_t pointSize,std::shared_ptr<OHOS::Media::PixelMap> pixelMap,int32_t maxDivergenceNum)30 KnuckleGlowTraceSystem::KnuckleGlowTraceSystem(int32_t pointSize, std::shared_ptr<OHOS::Media::PixelMap> pixelMap,
31 int32_t maxDivergenceNum) : maxDivergenceNum_(maxDivergenceNum)
32 {
33 CALL_DEBUG_ENTER;
34 CHKPV(pixelMap);
35 for (int32_t i = 0; i < pointSize; ++i) {
36 divergentPoints_.emplace_back(std::make_shared<KnuckleDivergentPoint>(pixelMap));
37 glowPoints_.emplace_back(std::make_shared<KnuckleGlowPoint>(pixelMap));
38 }
39 }
40
Clear()41 void KnuckleGlowTraceSystem::Clear()
42 {
43 CALL_DEBUG_ENTER;
44 for (const auto &divergentPoint : divergentPoints_) {
45 divergentPoint->Clear();
46 }
47 }
48
Update()49 void KnuckleGlowTraceSystem::Update()
50 {
51 CALL_DEBUG_ENTER;
52 for (size_t i = 0; i < glowPoints_.size(); i++) {
53 glowPoints_[i]->Update();
54 divergentPoints_[i]->Update();
55 }
56 }
57
Draw(Rosen::ExtendRecordingCanvas * canvas)58 void KnuckleGlowTraceSystem::Draw(Rosen::ExtendRecordingCanvas* canvas)
59 {
60 CALL_DEBUG_ENTER;
61 for (size_t i = 0; i < glowPoints_.size(); ++i) {
62 std::shared_ptr<KnuckleDivergentPoint> divergentPoint = divergentPoints_[i];
63 std::shared_ptr<KnuckleGlowPoint> glowPoint = glowPoints_[i];
64 if (divergentPoint != nullptr) {
65 divergentPoint->Draw(canvas);
66 }
67 if (glowPoint != nullptr) {
68 glowPoint->Draw(canvas);
69 }
70 }
71 }
72
ResetDivergentPoints(double pointX,double pointY)73 void KnuckleGlowTraceSystem::ResetDivergentPoints(double pointX, double pointY)
74 {
75 CALL_DEBUG_ENTER;
76 int32_t divergenceNum = 0;
77 for (const auto &divergentPoint : divergentPoints_) {
78 CHKPC(divergentPoint);
79 if (divergentPoint->IsEnded() && divergenceNum < maxDivergenceNum_) {
80 divergenceNum++;
81 divergentPoint->Reset(pointX, pointY);
82 }
83 }
84 }
85
AddGlowPoints(const Rosen::Drawing::Path & path,int64_t timeInterval)86 void KnuckleGlowTraceSystem::AddGlowPoints(const Rosen::Drawing::Path &path, int64_t timeInterval)
87 {
88 CALL_DEBUG_ENTER;
89 double pathlength = path.GetLength(false);
90 Rosen::Drawing::Point pathPoints;
91 Rosen::Drawing::Point tangent;
92 float distanceFromEnd = 0;
93 float lifespanOffset = timeInterval;
94 float splitRatio = static_cast<float>(std::ceil(pathlength / BASIC_DISTANCE_BETWEEN_POINTS));
95 float baseTime = timeInterval / splitRatio;
96 for (const auto &glowPoint : glowPoints_) {
97 if (glowPoint != nullptr && glowPoint->IsEnded() && distanceFromEnd <= pathlength) {
98 if (path.GetPositionAndTangent(distanceFromEnd, pathPoints, tangent, true)) {
99 glowPoint->Reset(pathPoints.GetX(), pathPoints.GetY(), lifespanOffset);
100 distanceFromEnd += BASIC_DISTANCE_BETWEEN_POINTS;
101 lifespanOffset -= baseTime;
102 }
103 }
104 }
105 }
106 } // namespace MMI
107 } // namespace OHOS
108