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_point.h"
17
18 #include "mmi_log.h"
19 #include "platform/ohos/overdraw/rs_overdraw_controller.h"
20
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "KnuckleGlowPoint"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr int32_t SEC_TO_NANOSEC { 1000000000 };
28 constexpr int32_t NANOSECOND_TO_MILLISECOND { 1000000 };
29 [[ maybe_unused ]] constexpr int32_t PAINT_WIDTH { 20 };
30 [[ maybe_unused ]] constexpr int32_t ARGB_A { 0 };
31 [[ maybe_unused ]] constexpr int32_t ARGB_RGB { 255 };
32 constexpr double BASIC_LIFESPAN { 200.0f };
33 [[ maybe_unused ]] constexpr int32_t TRACE_COLOR { 255 };
34 constexpr float BASIC_SIZE { 100.0f };
35 constexpr int32_t ARGB_COLOR_ARRAY { 0x11c8ffff };
36 constexpr double HALF { 2.0 };
37 } // namespace
38
KnuckleGlowPoint(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)39 KnuckleGlowPoint::KnuckleGlowPoint(std::shared_ptr<OHOS::Media::PixelMap> pixelMap) : traceShadow_(pixelMap)
40 {
41 OHOS::Rosen::Drawing::Filter filter;
42 OHOS::Rosen::OverdrawColorArray colorArray = {
43 0x00000000,
44 0x00000000,
45 0x00000000,
46 0x00000000,
47 0x00000000,
48 ARGB_COLOR_ARRAY,
49 };
50 auto protanomalyMat = OHOS::Rosen::Drawing::ColorFilter::CreateOverDrawColorFilter(colorArray.data());
51 filter.SetColorFilter(protanomalyMat);
52 OHOS::Rosen::Drawing::Brush brush;
53 brush_.SetFilter(filter);
54 }
55
GetNanoTime() const56 int64_t KnuckleGlowPoint::GetNanoTime() const
57 {
58 CALL_DEBUG_ENTER;
59 struct timespec time = { 0 };
60 clock_gettime(CLOCK_MONOTONIC, &time);
61 return static_cast<int64_t>(time.tv_sec) * SEC_TO_NANOSEC + time.tv_nsec;
62 }
63
Update()64 void KnuckleGlowPoint::Update()
65 {
66 CALL_DEBUG_ENTER;
67 if (IsEnded()) {
68 return;
69 }
70 int64_t currentTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
71 int64_t timeInterval = currentTime - lastUpdateTimeMillis_;
72 if (timeInterval < 0) {
73 timeInterval = 0;
74 }
75
76 lastUpdateTimeMillis_ = currentTime;
77 lifespan_ -= timeInterval;
78 traceSize_ = static_cast<float>((lifespan_ / BASIC_LIFESPAN) * BASIC_SIZE);
79 UpdateMatrix();
80 }
81
Draw(Rosen::ExtendRecordingCanvas * canvas)82 void KnuckleGlowPoint::Draw(Rosen::ExtendRecordingCanvas* canvas)
83 {
84 CALL_DEBUG_ENTER;
85 CHKPV(canvas);
86 CHKPV(traceShadow_);
87 if (IsEnded() || pointX_ <= 0 || pointY_ <= 0) {
88 return;
89 }
90 canvas->SetMatrix(traceMatrix_);
91 canvas->AttachBrush(brush_);
92 Rosen::Drawing::Rect src = Rosen::Drawing::Rect(0, 0, traceShadow_->GetWidth(), traceShadow_->GetHeight());
93 Rosen::Drawing::Rect dst = Rosen::Drawing::Rect(pointX_ - traceShadow_->GetWidth() / HALF,
94 pointY_ - traceShadow_->GetHeight() / HALF, pointX_ + traceShadow_->GetWidth() / HALF,
95 pointY_ + traceShadow_->GetHeight());
96 canvas->DrawPixelMapRect(traceShadow_, src, dst, Rosen::Drawing::SamplingOptions());
97 canvas->DetachBrush();
98 }
99
Reset(double pointX,double pointY,float lifespanOffset)100 void KnuckleGlowPoint::Reset(double pointX, double pointY, float lifespanOffset)
101 {
102 CALL_DEBUG_ENTER;
103 pointX_ = pointX;
104 pointY_ = pointY;
105 lifespan_ = BASIC_LIFESPAN - lifespanOffset;
106 traceSize_ = BASIC_SIZE;
107 lastUpdateTimeMillis_ = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
108 }
109
IsEnded() const110 bool KnuckleGlowPoint::IsEnded() const
111 {
112 CALL_DEBUG_ENTER;
113 return lifespan_ < 0;
114 }
115
UpdateMatrix()116 void KnuckleGlowPoint::UpdateMatrix()
117 {
118 CALL_DEBUG_ENTER;
119 CHKPV(traceShadow_);
120 traceMatrix_.Reset();
121 float proportion = traceSize_ / traceShadow_->GetWidth();
122 traceMatrix_.PostScale(proportion, proportion, pointX_, pointY_);
123 }
124 } // namespace MMI
125 } // namespace OHOS
126