106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
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 FPS_H
1606f6ba60Sopenharmony_ci#define FPS_H
1706f6ba60Sopenharmony_ci#include <vector>
1806f6ba60Sopenharmony_ci#include <queue>
1906f6ba60Sopenharmony_ci#include "sp_profiler.h"
2006f6ba60Sopenharmony_cinamespace OHOS {
2106f6ba60Sopenharmony_cinamespace SmartPerf {
2206f6ba60Sopenharmony_cistruct FpsInfo {
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 currTimeDump;
2806f6ba60Sopenharmony_ci    void Clear()
2906f6ba60Sopenharmony_ci    {
3006f6ba60Sopenharmony_ci        fps = 0;
3106f6ba60Sopenharmony_ci        curTime = 0;
3206f6ba60Sopenharmony_ci        jitters.clear();
3306f6ba60Sopenharmony_ci    }
3406f6ba60Sopenharmony_ci    bool operator == (const FpsInfo &other) const
3506f6ba60Sopenharmony_ci    {
3606f6ba60Sopenharmony_ci        if (fps != other.fps) {
3706f6ba60Sopenharmony_ci            return false;
3806f6ba60Sopenharmony_ci        }
3906f6ba60Sopenharmony_ci        if (jitters.size() != other.jitters.size()) {
4006f6ba60Sopenharmony_ci            return false;
4106f6ba60Sopenharmony_ci        }
4206f6ba60Sopenharmony_ci        for (size_t i = 0; i < jitters.size(); i++) {
4306f6ba60Sopenharmony_ci            if (jitters[i] != other.jitters[i]) {
4406f6ba60Sopenharmony_ci                return false;
4506f6ba60Sopenharmony_ci            }
4606f6ba60Sopenharmony_ci        }
4706f6ba60Sopenharmony_ci        return true;
4806f6ba60Sopenharmony_ci    }
4906f6ba60Sopenharmony_ci    FpsInfo()
5006f6ba60Sopenharmony_ci    {
5106f6ba60Sopenharmony_ci        fps = 0;
5206f6ba60Sopenharmony_ci        curTime = 0;
5306f6ba60Sopenharmony_ci        currTimeDump = 0;
5406f6ba60Sopenharmony_ci    }
5506f6ba60Sopenharmony_ci};
5606f6ba60Sopenharmony_cistruct FpsCurrentFpsTime {
5706f6ba60Sopenharmony_ci    int fps = 0;
5806f6ba60Sopenharmony_ci    long long currentFpsTime = 0;
5906f6ba60Sopenharmony_ci};
6006f6ba60Sopenharmony_ci
6106f6ba60Sopenharmony_ciclass FPS : public SpProfiler {
6206f6ba60Sopenharmony_cipublic:
6306f6ba60Sopenharmony_ci    void SetPackageName(std::string pName);
6406f6ba60Sopenharmony_ci    void SetLayerName(std::string sName);
6506f6ba60Sopenharmony_ci    FpsInfo GetFpsInfo();
6606f6ba60Sopenharmony_ci    FpsInfo GetDiffLayersFpsInfo(const std::string &sName);
6706f6ba60Sopenharmony_ci    void CalcFpsAndJitters();
6806f6ba60Sopenharmony_ci    FpsInfo fpsInfo;
6906f6ba60Sopenharmony_ci    FpsInfo fpsInfoMax;
7006f6ba60Sopenharmony_ci    FpsInfo prevResultFpsInfo;
7106f6ba60Sopenharmony_ci    static FPS &GetInstance()
7206f6ba60Sopenharmony_ci    {
7306f6ba60Sopenharmony_ci        static FPS instance;
7406f6ba60Sopenharmony_ci        return instance;
7506f6ba60Sopenharmony_ci    }
7606f6ba60Sopenharmony_ci    std::map<std::string, std::string> ItemData() override;
7706f6ba60Sopenharmony_ci    void SetFpsCurrentFpsTime(FpsInfo fpsInfoResult);
7806f6ba60Sopenharmony_ci    FpsCurrentFpsTime GetFpsCurrentFpsTime();
7906f6ba60Sopenharmony_ci    void ReadDataFromPipe(int fd);
8006f6ba60Sopenharmony_ci    void SetProcessId(const std::string &pid);
8106f6ba60Sopenharmony_ci    void SetRkFlag();
8206f6ba60Sopenharmony_ci    std::string FindFpsRefreshrate();
8306f6ba60Sopenharmony_ciprivate:
8406f6ba60Sopenharmony_ci    FPS() {};
8506f6ba60Sopenharmony_ci    FPS(const FPS &);
8606f6ba60Sopenharmony_ci    FPS &operator = (const FPS &);
8706f6ba60Sopenharmony_ci
8806f6ba60Sopenharmony_ci    std::string pkgName;
8906f6ba60Sopenharmony_ci    std::string surfaceViewName;
9006f6ba60Sopenharmony_ci    bool refresh = false;
9106f6ba60Sopenharmony_ci    long long mod = 1e9;
9206f6ba60Sopenharmony_ci    long long curScreenTimestamp = 0;
9306f6ba60Sopenharmony_ci    long long prevScreenTimestamp = -1;
9406f6ba60Sopenharmony_ci    long long prevlastScreenTimestamp = 0;
9506f6ba60Sopenharmony_ci    int fpsNum = 0;
9606f6ba60Sopenharmony_ci    FpsInfo GetSurfaceFrame(std::string name);
9706f6ba60Sopenharmony_ci    int fifty = 50;
9806f6ba60Sopenharmony_ci    FpsCurrentFpsTime ffTime;
9906f6ba60Sopenharmony_ci    bool processFlag = false;
10006f6ba60Sopenharmony_ci    bool rkFlag = false;
10106f6ba60Sopenharmony_ci    const std::string screenPath = "/sys/class/graphics/fb0/lcd_fps_scence";
10206f6ba60Sopenharmony_ci    std::string processId = "";
10306f6ba60Sopenharmony_ci};
10406f6ba60Sopenharmony_ci}
10506f6ba60Sopenharmony_ci}
10606f6ba60Sopenharmony_ci#endif
107