1/*
2 * Copyright (C) 2021 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 FPS_H
16#define FPS_H
17#include <vector>
18#include <queue>
19#include "sp_profiler.h"
20namespace OHOS {
21namespace SmartPerf {
22struct FpsInfo {
23    int fps;
24    std::vector<long long> jitters;
25    std::vector<long long> currTimeStamps;
26    int curTime;
27    long long currTimeDump;
28    void Clear()
29    {
30        fps = 0;
31        curTime = 0;
32        jitters.clear();
33    }
34    bool operator == (const FpsInfo &other) const
35    {
36        if (fps != other.fps) {
37            return false;
38        }
39        if (jitters.size() != other.jitters.size()) {
40            return false;
41        }
42        for (size_t i = 0; i < jitters.size(); i++) {
43            if (jitters[i] != other.jitters[i]) {
44                return false;
45            }
46        }
47        return true;
48    }
49    FpsInfo()
50    {
51        fps = 0;
52        curTime = 0;
53        currTimeDump = 0;
54    }
55};
56struct FpsCurrentFpsTime {
57    int fps = 0;
58    long long currentFpsTime = 0;
59};
60
61class FPS : public SpProfiler {
62public:
63    void SetPackageName(std::string pName);
64    void SetLayerName(std::string sName);
65    FpsInfo GetFpsInfo();
66    FpsInfo GetDiffLayersFpsInfo(const std::string &sName);
67    void CalcFpsAndJitters();
68    FpsInfo fpsInfo;
69    FpsInfo fpsInfoMax;
70    FpsInfo prevResultFpsInfo;
71    static FPS &GetInstance()
72    {
73        static FPS instance;
74        return instance;
75    }
76    std::map<std::string, std::string> ItemData() override;
77    void SetFpsCurrentFpsTime(FpsInfo fpsInfoResult);
78    FpsCurrentFpsTime GetFpsCurrentFpsTime();
79    void ReadDataFromPipe(int fd);
80    void SetProcessId(const std::string &pid);
81    void SetRkFlag();
82    std::string FindFpsRefreshrate();
83private:
84    FPS() {};
85    FPS(const FPS &);
86    FPS &operator = (const FPS &);
87
88    std::string pkgName;
89    std::string surfaceViewName;
90    bool refresh = false;
91    long long mod = 1e9;
92    long long curScreenTimestamp = 0;
93    long long prevScreenTimestamp = -1;
94    long long prevlastScreenTimestamp = 0;
95    int fpsNum = 0;
96    FpsInfo GetSurfaceFrame(std::string name);
97    int fifty = 50;
98    FpsCurrentFpsTime ffTime;
99    bool processFlag = false;
100    bool rkFlag = false;
101    const std::string screenPath = "/sys/class/graphics/fb0/lcd_fps_scence";
102    std::string processId = "";
103};
104}
105}
106#endif
107