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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_RESAMPLE_ALGO_H 16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_RESAMPLE_ALGO_H 17 #include <vector> 18 #include "core/event/axis_event.h" 19 20 namespace OHOS::Ace { 21 namespace { 22 constexpr int32_t RESAMPLE_COORD_TIME_THRESHOLD = 20 * 1000 * 1000; 23 constexpr uint64_t INTERPOLATION_THRESHOLD = 100 * 1000 * 1000; // 100ms 24 constexpr uint64_t ONE_S_IN_NS = 1 * 1000 * 1000 * 1000; 25 } 26 27 struct AvgPoint { 28 float x = {}; 29 float y = {}; 30 uint64_t time = {}; 31 float windowX = {}; 32 float windowY = {}; 33 34 AvgPoint& operator+=(const AvgPoint &other) 35 { 36 x += other.x; 37 y += other.y; 38 windowX += other.windowX; 39 windowY += other.windowY; 40 time += other.time; 41 return *this; 42 } 43 44 AvgPoint& operator/=(float divisor) 45 { 46 if (NearEqual(divisor, 0.0f)) { 47 return *this; 48 } 49 x /= divisor; 50 y /= divisor; 51 windowX /= divisor; 52 windowY /= divisor; 53 time /= static_cast<uint64_t>(divisor); 54 return *this; 55 } 56 }; 57 58 enum class CoordinateType : int32_t { 59 NORMAL = 0, 60 SCREEN = 1, 61 GLOBALDISPLAY = 2, 62 }; 63 64 struct ResamplePoint { 65 float x{}; 66 float y{}; 67 float inputXDeltaSlope{}; 68 float inputYDeltaSlope{}; 69 }; 70 71 class ResampleAlgo final { 72 public: 73 ResampleAlgo() = delete; 74 75 ~ResampleAlgo() = delete; 76 77 static AvgPoint GetAvgPoint(const std::vector<PointerEvent>&& events, 78 CoordinateType coordinateType); 79 80 static ResamplePoint LinearInterpolation(const AvgPoint& history, const AvgPoint& current, 81 uint64_t nanoTimeStamp); 82 83 static ResamplePoint GetResampleCoord(const std::vector<PointerEvent>&& history, 84 const std::vector<PointerEvent>&& current, uint64_t nanoTimeStamp, 85 CoordinateType coordinateType); 86 87 template<class T> 88 static bool GetResamplePointerEvent(std::vector<T>& events, 89 uint64_t resampleTime, PointerEvent& resample, ResamplePoint& slope); 90 }; 91 } // namespace OHOS::Ace 92 #endif