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 
16 #ifndef ROSEN_MODULE_FRAME_ANALYZER_EXPORT_FRAME_COLLECTOR_H
17 #define ROSEN_MODULE_FRAME_ANALYZER_EXPORT_FRAME_COLLECTOR_H
18 
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 
23 #include "frame_info.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class FrameSaver;
28 
29 class FrameCollector {
30 public:
31     static FrameCollector &GetInstance();
32 
33     ~FrameCollector() = default;
34 
35     void SetRepaintCallback(std::function<void()> repaint);
36     const FrameInfoQueue &LockGetFrameQueue();
37     void UnlockFrameQueue();
38     bool IsEnabled() const;
39     void SetEnabled(bool enable);
40     void MarkFrameEvent(const FrameEventType &type, int64_t timeNs = 0);
41     void ClearEvents();
42 
43 private:
44     FrameCollector();
45     FrameCollector(const FrameCollector &collector) = delete;
46     FrameCollector(FrameCollector &&collector) = delete;
47     FrameCollector &operator =(const FrameCollector &collector) = delete;
48     FrameCollector &operator =(FrameCollector &&collector) = delete;
49 
50     void ProcessFrameEvent(int32_t index, int64_t timeNs);
51     bool ProcessUIMarkLocked(int32_t index, int64_t timeNs);
52     static void SwitchFunction(const char *key, const char *value, void *context);
53 
54     // pending
55     std::mutex pendingMutex_;
56     int32_t currentFrameNumber_ = 0;
57     struct UIMarks pendingUIMarks_ = {};
58     struct UIMarks currentUIMarks_ = {};
59     struct FrameInfo *pbefore_ = nullptr;
60     struct FrameInfo *pafter_ = nullptr;
61     bool haveAfterVsync_ = false;
62 
63     // frame queue
64     std::mutex frameQueueMutex_;
65     FrameInfoQueue frameQueue_;
66 
67     // param
68     bool enabled_ = false;
69     bool usingSaver_ = false;
70     std::shared_ptr<FrameSaver> saver_ = nullptr;
71     std::function<void()> repaint_ = nullptr;
72 };
73 } // namespace Rosen
74 } // namespace OHOS
75 
76 #endif // ROSEN_MODULE_FRAME_ANALYZER_EXPORT_FRAME_COLLECTOR_H
77