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 
16 #ifndef UTILS_INCLUDE_FRAME_REPORT_H
17 #define UTILS_INCLUDE_FRAME_REPORT_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <unordered_map>
22 #include <shared_mutex>
23 
24 namespace OHOS {
25 namespace Rosen {
26 
27 using NotifyFrameInfoFunc = bool(*)(int32_t, const std::string&, int64_t, const std::string&);
28 constexpr int32_t FR_DEFAULT_PID = 0;
29 constexpr uint64_t FR_DEFAULT_UNIQUEID = 0;
30 
31 class FrameReport {
32 public:
33     static FrameReport& GetInstance();
34 
35     void SetGameScene(int32_t pid, int32_t state);
36     bool HasGameScene();
37     bool IsActiveGameWithPid(int32_t pid);
38     bool IsActiveGameWithUniqueId(uint64_t uniqueId);
39     void SetLastSwapBufferTime(int64_t lastSwapBufferTime);
40     void SetDequeueBufferTime(const std::string& layerName, int64_t dequeueBufferTime);
41     void SetQueueBufferTime(uint64_t uniqueId, const std::string& layerName, int64_t queueBufferTime);
42     void SetPendingBufferNum(const std::string& layerName, int32_t pendingBufferNum);
43     void Report(const std::string& layerName);
44 
45 private:
46     FrameReport();
47     ~FrameReport();
48 
49     void LoadLibrary();
50     void CloseLibrary();
51     void* LoadSymbol(const std::string& symName);
52 
53     void DeletePidInfo();
54     void NotifyFrameInfo(int32_t pid, const std::string& layerName, int64_t timeStamp, const std::string& bufferMsg);
55 
56     std::atomic<int32_t> activelyPid_ = FR_DEFAULT_PID;
57     std::atomic<uint64_t> activelyUniqueId_ = FR_DEFAULT_UNIQUEID;
58     std::atomic<int32_t> pendingBufferNum_ = 0;
59     std::atomic<int64_t> lastSwapBufferTime_ = 0;
60     std::atomic<int64_t> dequeueBufferTime_ = 0;
61     std::atomic<int64_t> queueBufferTime_ = 0;
62 
63     bool isGameSoLoaded_ = false;
64     void* gameSoHandle_ = nullptr;
65     NotifyFrameInfoFunc notifyFrameInfoFunc_ = nullptr;
66 
67     mutable std::shared_mutex mutex_;
68 };
69 } // namespace Rosen
70 } // namespace OHOS
71 
72 #endif // UTILS_INCLUDE_FRAME_REPORT_H
73