106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#ifndef PLUGNFPS_H
1606f6ba60Sopenharmony_ci#define PLUGNFPS_H
1706f6ba60Sopenharmony_ci#include <vector>
1806f6ba60Sopenharmony_ci#include "sp_profiler.h"
1906f6ba60Sopenharmony_ci#include <queue>
2006f6ba60Sopenharmony_cinamespace OHOS {
2106f6ba60Sopenharmony_cinamespace SmartPerf {
2206f6ba60Sopenharmony_cistruct FpsInfoProfiler {
2306f6ba60Sopenharmony_ci    int fps;
2406f6ba60Sopenharmony_ci    std::vector<long long> jitters;
2506f6ba60Sopenharmony_ci    std::vector<long long> currTimeStamps;
2606f6ba60Sopenharmony_ci    int curTime;
2706f6ba60Sopenharmony_ci    long long currTimeDiff;
2806f6ba60Sopenharmony_ci    long long currTimeDump;
2906f6ba60Sopenharmony_ci    void Clear()
3006f6ba60Sopenharmony_ci    {
3106f6ba60Sopenharmony_ci        fps = 0;
3206f6ba60Sopenharmony_ci        jitters.clear();
3306f6ba60Sopenharmony_ci        currTimeDiff = 0;
3406f6ba60Sopenharmony_ci        currTimeDump = 0;
3506f6ba60Sopenharmony_ci    }
3606f6ba60Sopenharmony_ci    bool operator == (const FpsInfoProfiler &other) const
3706f6ba60Sopenharmony_ci    {
3806f6ba60Sopenharmony_ci        if (fps != other.fps) {
3906f6ba60Sopenharmony_ci            return false;
4006f6ba60Sopenharmony_ci        }
4106f6ba60Sopenharmony_ci        if (jitters.size() != other.jitters.size()) {
4206f6ba60Sopenharmony_ci            return false;
4306f6ba60Sopenharmony_ci        }
4406f6ba60Sopenharmony_ci        for (size_t i = 0; i < jitters.size(); i++) {
4506f6ba60Sopenharmony_ci            if (jitters[i] != other.jitters[i]) {
4606f6ba60Sopenharmony_ci                return false;
4706f6ba60Sopenharmony_ci            }
4806f6ba60Sopenharmony_ci        }
4906f6ba60Sopenharmony_ci        return true;
5006f6ba60Sopenharmony_ci    }
5106f6ba60Sopenharmony_ci    FpsInfoProfiler()
5206f6ba60Sopenharmony_ci    {
5306f6ba60Sopenharmony_ci        fps = 0;
5406f6ba60Sopenharmony_ci        curTime = 0;
5506f6ba60Sopenharmony_ci        currTimeDiff = 0;
5606f6ba60Sopenharmony_ci        currTimeDump = 0;
5706f6ba60Sopenharmony_ci    }
5806f6ba60Sopenharmony_ci};
5906f6ba60Sopenharmony_ciclass ProfilerFPS : public SpProfiler {
6006f6ba60Sopenharmony_cipublic:
6106f6ba60Sopenharmony_ci    void SetTraceCatch();
6206f6ba60Sopenharmony_ci    void SetGameLayer(std::string isGameView);
6306f6ba60Sopenharmony_ci    std::string GetGameLayer();
6406f6ba60Sopenharmony_ci    void SetPackageName(std::string pName);
6506f6ba60Sopenharmony_ci    void GetFPS(std::vector<std::string> v);
6606f6ba60Sopenharmony_ci    void GetResultFPS(int sectionsNum);
6706f6ba60Sopenharmony_ci    FpsInfoProfiler GetFpsInfo();
6806f6ba60Sopenharmony_ci    FpsInfoProfiler selfRerderLayers(const std::string &gameLayer);
6906f6ba60Sopenharmony_ci    bool IsForeGround();
7006f6ba60Sopenharmony_ci    bool IsFindForeGround(std::string line) const;
7106f6ba60Sopenharmony_ci    void CalcFpsAndJitters();
7206f6ba60Sopenharmony_ci    FpsInfoProfiler GetSurfaceFrame(std::string name);
7306f6ba60Sopenharmony_ci    void PrintSections(int msCount, long long currTimeLast, long long currTimeStart, long long currLastTime) const;
7406f6ba60Sopenharmony_ci    void GetSectionsFps(FpsInfoProfiler &fpsInfoResult, int nums) const;
7506f6ba60Sopenharmony_ci    void GetSectionsPrint(int printCount, long long msStartTime, int numb, long long harTime) const;
7606f6ba60Sopenharmony_ci    void GetTimeDiff();
7706f6ba60Sopenharmony_ci    void GetCurrentTime(int sleepNum);
7806f6ba60Sopenharmony_ci    void GetOhFps(std::vector<std::string> v);
7906f6ba60Sopenharmony_ci    void SetRkFlag();
8006f6ba60Sopenharmony_ci    std::string GetSurface();
8106f6ba60Sopenharmony_ci    FpsInfoProfiler fpsInfo;
8206f6ba60Sopenharmony_ci    FpsInfoProfiler fpsInfoMax;
8306f6ba60Sopenharmony_ci    FpsInfoProfiler lastFpsInfoResult;
8406f6ba60Sopenharmony_ci    static ProfilerFPS &GetInstance()
8506f6ba60Sopenharmony_ci    {
8606f6ba60Sopenharmony_ci        static ProfilerFPS instance;
8706f6ba60Sopenharmony_ci        return instance;
8806f6ba60Sopenharmony_ci    }
8906f6ba60Sopenharmony_ci    std::map<std::string, std::string> ItemData() override;
9006f6ba60Sopenharmony_ci    void SetProcessId(const std::string &pid);
9106f6ba60Sopenharmony_ciprivate:
9206f6ba60Sopenharmony_ci    ProfilerFPS() {};
9306f6ba60Sopenharmony_ci    ProfilerFPS(const ProfilerFPS &);
9406f6ba60Sopenharmony_ci    ProfilerFPS &operator = (const ProfilerFPS &);
9506f6ba60Sopenharmony_ci
9606f6ba60Sopenharmony_ci    std::string pkgName;
9706f6ba60Sopenharmony_ci    int num = 1;
9806f6ba60Sopenharmony_ci    int number = 2;
9906f6ba60Sopenharmony_ci    bool refresh = false;
10006f6ba60Sopenharmony_ci    long long mod = 1e9;
10106f6ba60Sopenharmony_ci    long long curScreenTimestamp = 0;
10206f6ba60Sopenharmony_ci    long long prevScreenTimestamp = -1;
10306f6ba60Sopenharmony_ci    long long prevlastScreenTimestamp = 0;
10406f6ba60Sopenharmony_ci    int fpsNum = 0;
10506f6ba60Sopenharmony_ci    bool isFirstResult = false;
10606f6ba60Sopenharmony_ci    long oneSec = 1000000;
10706f6ba60Sopenharmony_ci    unsigned long sleepTime = 950000;
10806f6ba60Sopenharmony_ci    unsigned long sleepNowTime = 10000;
10906f6ba60Sopenharmony_ci    int ten = 10;
11006f6ba60Sopenharmony_ci    int four = 4;
11106f6ba60Sopenharmony_ci    int fifty = 50;
11206f6ba60Sopenharmony_ci    long long lastCurrTime = 0;
11306f6ba60Sopenharmony_ci    long long oneThousand = 1000;
11406f6ba60Sopenharmony_ci    long long msClear = 1000000000;
11506f6ba60Sopenharmony_ci    long long currRealTime = 0;
11606f6ba60Sopenharmony_ci    bool isFoundAppName = false;
11706f6ba60Sopenharmony_ci    bool isFoundBundleName = false;
11806f6ba60Sopenharmony_ci    int isCatchTrace = 0;
11906f6ba60Sopenharmony_ci    std::string isGameLayer = "";
12006f6ba60Sopenharmony_ci    bool processFlag = false;
12106f6ba60Sopenharmony_ci    bool ohFlag = false;
12206f6ba60Sopenharmony_ci    std::string processId = "";
12306f6ba60Sopenharmony_ci    bool rkFlag = false;
12406f6ba60Sopenharmony_ci};
12506f6ba60Sopenharmony_ci}
12606f6ba60Sopenharmony_ci}
12706f6ba60Sopenharmony_ci#endif