1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 PLUGNFPS_H
16#define PLUGNFPS_H
17#include <vector>
18#include "sp_profiler.h"
19#include <queue>
20namespace OHOS {
21namespace SmartPerf {
22struct FpsInfoProfiler {
23    int fps;
24    std::vector<long long> jitters;
25    std::vector<long long> currTimeStamps;
26    int curTime;
27    long long currTimeDiff;
28    long long currTimeDump;
29    void Clear()
30    {
31        fps = 0;
32        jitters.clear();
33        currTimeDiff = 0;
34        currTimeDump = 0;
35    }
36    bool operator == (const FpsInfoProfiler &other) const
37    {
38        if (fps != other.fps) {
39            return false;
40        }
41        if (jitters.size() != other.jitters.size()) {
42            return false;
43        }
44        for (size_t i = 0; i < jitters.size(); i++) {
45            if (jitters[i] != other.jitters[i]) {
46                return false;
47            }
48        }
49        return true;
50    }
51    FpsInfoProfiler()
52    {
53        fps = 0;
54        curTime = 0;
55        currTimeDiff = 0;
56        currTimeDump = 0;
57    }
58};
59class ProfilerFPS : public SpProfiler {
60public:
61    void SetTraceCatch();
62    void SetGameLayer(std::string isGameView);
63    std::string GetGameLayer();
64    void SetPackageName(std::string pName);
65    void GetFPS(std::vector<std::string> v);
66    void GetResultFPS(int sectionsNum);
67    FpsInfoProfiler GetFpsInfo();
68    FpsInfoProfiler selfRerderLayers(const std::string &gameLayer);
69    bool IsForeGround();
70    bool IsFindForeGround(std::string line) const;
71    void CalcFpsAndJitters();
72    FpsInfoProfiler GetSurfaceFrame(std::string name);
73    void PrintSections(int msCount, long long currTimeLast, long long currTimeStart, long long currLastTime) const;
74    void GetSectionsFps(FpsInfoProfiler &fpsInfoResult, int nums) const;
75    void GetSectionsPrint(int printCount, long long msStartTime, int numb, long long harTime) const;
76    void GetTimeDiff();
77    void GetCurrentTime(int sleepNum);
78    void GetOhFps(std::vector<std::string> v);
79    void SetRkFlag();
80    std::string GetSurface();
81    FpsInfoProfiler fpsInfo;
82    FpsInfoProfiler fpsInfoMax;
83    FpsInfoProfiler lastFpsInfoResult;
84    static ProfilerFPS &GetInstance()
85    {
86        static ProfilerFPS instance;
87        return instance;
88    }
89    std::map<std::string, std::string> ItemData() override;
90    void SetProcessId(const std::string &pid);
91private:
92    ProfilerFPS() {};
93    ProfilerFPS(const ProfilerFPS &);
94    ProfilerFPS &operator = (const ProfilerFPS &);
95
96    std::string pkgName;
97    int num = 1;
98    int number = 2;
99    bool refresh = false;
100    long long mod = 1e9;
101    long long curScreenTimestamp = 0;
102    long long prevScreenTimestamp = -1;
103    long long prevlastScreenTimestamp = 0;
104    int fpsNum = 0;
105    bool isFirstResult = false;
106    long oneSec = 1000000;
107    unsigned long sleepTime = 950000;
108    unsigned long sleepNowTime = 10000;
109    int ten = 10;
110    int four = 4;
111    int fifty = 50;
112    long long lastCurrTime = 0;
113    long long oneThousand = 1000;
114    long long msClear = 1000000000;
115    long long currRealTime = 0;
116    bool isFoundAppName = false;
117    bool isFoundBundleName = false;
118    int isCatchTrace = 0;
119    std::string isGameLayer = "";
120    bool processFlag = false;
121    bool ohFlag = false;
122    std::string processId = "";
123    bool rkFlag = false;
124};
125}
126}
127#endif