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 RS_GPU_DIRTY_REGION_COLLECTION_H
17 #define RS_GPU_DIRTY_REGION_COLLECTION_H
18 
19 #include <mutex>
20 
21 #include "common/rs_common_def.h"
22 #include "common/rs_rect.h"
23 #include "surface_type.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 
28 struct ActiveDirtyRegionInfo {
29     int64_t activeDirtyRegionArea = 0;
30     int32_t activeFramesNumber = 0;
31     int32_t pidOfBelongsApp = 0;
32     std::string windowName;
ActiveDirtyRegionInfoActiveDirtyRegionInfo33     ActiveDirtyRegionInfo()
34         : activeDirtyRegionArea(), activeFramesNumber(), pidOfBelongsApp(), windowName() {}
ActiveDirtyRegionInfoActiveDirtyRegionInfo35     ActiveDirtyRegionInfo(int64_t activeDirtyRegionArea_, int32_t activeFramesNumber_, int32_t pidOfBelongsApp_,
36         std::string windowName_)
37         : activeDirtyRegionArea(activeDirtyRegionArea_), activeFramesNumber(activeFramesNumber_),
38           pidOfBelongsApp(pidOfBelongsApp_), windowName(windowName_) {}
39 };
40 
41 struct GlobalDirtyRegionInfo {
42     int64_t globalDirtyRegionAreas = 0;
43     int32_t globalFramesNumber = 0;
44     int32_t skipProcessFramesNumber = 0;
45     pid_t mostSendingPidWhenDisplayNodeSkip = 0;
GlobalDirtyRegionInfoGlobalDirtyRegionInfo46     GlobalDirtyRegionInfo()
47         : globalDirtyRegionAreas(), globalFramesNumber(), skipProcessFramesNumber(),
48           mostSendingPidWhenDisplayNodeSkip() {}
GlobalDirtyRegionInfoGlobalDirtyRegionInfo49     GlobalDirtyRegionInfo(int64_t globalDirtyRegionAreas_, int32_t globalFramesNumber_,
50         int32_t skipProcessFramesNumber_, pid_t mostSendingPidWhenDisplayNodeSkip_)
51         : globalDirtyRegionAreas(globalDirtyRegionAreas_), globalFramesNumber(globalFramesNumber_),
52           skipProcessFramesNumber(skipProcessFramesNumber_),
53           mostSendingPidWhenDisplayNodeSkip(mostSendingPidWhenDisplayNodeSkip_) {}
54 };
55 
56 class RSB_EXPORT GpuDirtyRegionCollection {
57 public:
58     static GpuDirtyRegionCollection& GetInstance();
59 
60     void UpdateActiveDirtyInfoForDFX(NodeId id, const std::string& windowName, std::vector<RectI> rectIs);
61     void UpdateActiveDirtyInfoForDFX(NodeId id, const std::string& windowName, Rect damage);
62     void UpdateGlobalDirtyInfoForDFX(RectI rect);
63     void AddSkipProcessFramesNumberForDFX(pid_t sendingPid);
64     std::vector<ActiveDirtyRegionInfo> GetActiveDirtyRegionInfo() const;
65     GlobalDirtyRegionInfo GetGlobalDirtyRegionInfo() const;
66     void ResetActiveDirtyRegionInfo();
67     void ResetGlobalDirtyRegionInfo();
68 
69 private:
70     GpuDirtyRegionCollection();
71     ~GpuDirtyRegionCollection() noexcept;
72     GpuDirtyRegionCollection(const GpuDirtyRegionCollection&) = delete;
73     GpuDirtyRegionCollection(const GpuDirtyRegionCollection&&) = delete;
74     GpuDirtyRegionCollection& operator=(const GpuDirtyRegionCollection&) = delete;
75     GpuDirtyRegionCollection& operator=(const GpuDirtyRegionCollection&&) = delete;
76 
77     pid_t GetMostSendingPidWhenDisplayNodeSkip() const;
78 
79     std::unordered_map<NodeId, ActiveDirtyRegionInfo> activeDirtyRegionInfoMap_;
80     std::unordered_map<pid_t, int32_t> sendingPidWhenDisplayNodeSkipMap_;
81     GlobalDirtyRegionInfo globalDirtyRegionInfo_;
82     mutable std::mutex activeMtx_;
83     mutable std::mutex globalMtx_;
84 };
85 } // namespace Rosen
86 } // namespace OHOS
87 
88 #endif // RS_GPU_DIRTY_REGION_COLLECTION_H