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 HDI_BACKEND_HDI_LAYER_H
17 #define HDI_BACKEND_HDI_LAYER_H
18 
19 #include <array>
20 #include <stdint.h>
21 #include <surface.h>
22 #include "hdi_device.h"
23 #include "hdi_layer_info.h"
24 
25 
26 namespace OHOS {
27 namespace Rosen {
28 
29 using LayerInfoPtr = std::shared_ptr<HdiLayerInfo>;
30 struct FPSInfo {
31     int64_t presentTime;
32     std::vector<std::string> windowsName;
33 };
34 
35 class HdiLayer {
36 public:
37     explicit HdiLayer(uint32_t screenId);
38     virtual ~HdiLayer();
39 
40     static constexpr int FRAME_RECORDS_NUM = 256;
41 
42     /* output create and set layer info */
43     static std::shared_ptr<HdiLayer> CreateHdiLayer(uint32_t screenId);
44 
45     bool Init(const LayerInfoPtr &layerInfo);
46     void MergeWithFramebufferFence(const sptr<SyncFence> &fbAcquireFence);
47     void MergeWithLayerFence(const sptr<SyncFence> &layerReleaseFence);
48     void UpdateCompositionType(GraphicCompositionType type);
49 
50     const LayerInfoPtr GetLayerInfo();
51     void SetLayerStatus(bool inUsing);
52     bool GetLayerStatus() const;
53     void UpdateLayerInfo(const LayerInfoPtr &layerInfo);
54     int32_t SetHdiLayerInfo();
55     uint32_t GetLayerId() const;
56     bool RecordPresentTime(int64_t timestamp);
57     void RecordMergedPresentTime(int64_t timestamp); // used for uni render layer
58     void Dump(std::string &result);
59     void DumpMergedResult(std::string &result);  // used for uni render layer
60     void ClearDump();
61 
62     void SetReleaseFence(const sptr<SyncFence> &layerReleaseFence);
63     sptr<SyncFence> GetReleaseFence() const;
64     void SavePrevLayerInfo();
65     void DumpByName(std::string windowName, std::string &result);
66     void SelectHitchsInfo(std::string windowName, std::string &result);
67 
68     /* only used for mock tests */
69     int32_t SetHdiDeviceMock(HdiDevice* hdiDeviceMock);
70 private:
71     // layer buffer & fence
72     class LayerBufferInfo : public RefBase {
73     public:
74         LayerBufferInfo() = default;
75         virtual ~LayerBufferInfo() = default;
76 
77         sptr<SurfaceBuffer> sbuffer_ = nullptr;
78         sptr<SyncFence> releaseFence_ = SyncFence::InvalidFence();
79     };
80 
81     std::array<FPSInfo, FRAME_RECORDS_NUM> presentTimeRecords_ {};
82     uint32_t count_ = 0;
83     std::array<int64_t, FRAME_RECORDS_NUM> mergedPresentTimeRecords_ {}; // used for uni render layer
84     uint32_t mergedCount_ = 0; // used for uni render layer
85     uint32_t screenId_ = INT_MAX;
86     uint32_t layerId_ = INT_MAX;
87     bool isInUsing_ = false;
88     sptr<LayerBufferInfo> currBufferInfo_ = nullptr;
89     sptr<SurfaceBuffer> prevSbuffer_ = nullptr;
90     LayerInfoPtr layerInfo_ = nullptr;
91     LayerInfoPtr prevLayerInfo_ = nullptr;
92     GraphicPresentTimestampType supportedPresentTimestamptype_ = GRAPHIC_DISPLAY_PTS_UNSUPPORTED;
93     HdiDevice *device_ = nullptr;
94     bool doLayerInfoCompare_ = false;
95 
96     std::vector<uint32_t> bufferCache_;
97     uint32_t bufferCacheCountMax_ = 0;
98     mutable std::mutex mutex_;
99 
100     int32_t CreateLayer(const LayerInfoPtr &layerInfo);
101     void CloseLayer();
102     int32_t SetLayerAlpha();
103     int32_t SetLayerSize();
104     int32_t SetTransformMode();
105     int32_t SetLayerVisibleRegion();
106     int32_t SetLayerDirtyRegion();
107     int32_t SetLayerBuffer();
108     int32_t SetLayerCompositionType();
109     int32_t SetLayerBlendType();
110     int32_t SetLayerCrop();
111     int32_t SetLayerZorder();
112     int32_t SetLayerPreMulti();
113     int32_t SetLayerColor();
114     int32_t SetLayerColorTransform();
115     int32_t SetLayerColorDataSpace();
116     int32_t SetLayerMetaData();
117     int32_t SetLayerMetaDataSet();
118     sptr<SyncFence> Merge(const sptr<SyncFence> &fence1, const sptr<SyncFence> &fence2);
119     int32_t SetLayerTunnelHandle();
120     int32_t SetLayerPresentTimestamp();
121     int32_t InitDevice();
122     bool IsSameLayerMetaData();
123     bool IsSameLayerMetaDataSet();
124     inline void CheckRet(int32_t ret, const char* func);
125     int32_t SetLayerMaskInfo();
126     bool CheckAndUpdateLayerBufferCahce(uint32_t sequence, uint32_t& index,
127                                         std::vector<uint32_t>& deletingList);
128 
129     int32_t SetPerFrameParameters();
130     int32_t SetPerFrameParameterDisplayNit();
131     int32_t SetPerFrameParameterBrightnessRatio();
132     int32_t SetPerFrameLayerSourceTuning(); // used for source crop tuning
133     void ClearBufferCache();
134 };
135 } // namespace Rosen
136 } // namespace OHOS
137 
138 #endif // HDI_BACKEND_HDI_LAYER_H