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_OUTPUT_H
17 #define HDI_BACKEND_HDI_OUTPUT_H
18 
19 #include <array>
20 #include <stdint.h>
21 #include <vector>
22 #include <unordered_map>
23 
24 #include "graphic_error.h"
25 #include "surface_type.h"
26 #include "hdi_layer.h"
27 #include "hdi_framebuffer_surface.h"
28 #include "hdi_screen.h"
29 #include "vsync_sampler.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 
34 using LayerPtr = std::shared_ptr<HdiLayer>;
35 
36 // dump layer
37 struct LayerDumpInfo {
38     uint64_t nodeId;
39     uint64_t surfaceId;
40     LayerPtr layer;
41 };
42 
43 class HdiOutput {
44 public:
45     HdiOutput(uint32_t screenId);
46     virtual ~HdiOutput();
47 
48     static constexpr uint32_t COMPOSITION_RECORDS_NUM = HdiLayer::FRAME_RECORDS_NUM;
49 
50     /* for RS begin */
51     void SetLayerInfo(const std::vector<LayerInfoPtr> &layerInfos);
52     void SetOutputDamages(const std::vector<GraphicIRect> &outputDamages);
53     uint32_t GetScreenId() const;
54     // only used when composer_host dead
ResetDevice()55     void ResetDevice()
56     {
57         device_ = nullptr;
58     }
IsDeviceValid()59     bool IsDeviceValid() const
60     {
61         return device_ != nullptr;
62     }
63     /* for RS end */
64 
65     static std::shared_ptr<HdiOutput> CreateHdiOutput(uint32_t screenId);
66     RosenError Init();
67     void GetLayerInfos(std::vector<LayerInfoPtr>& layerInfos);
68     void GetComposeClientLayers(std::vector<LayerPtr>& clientLayers);
69     const std::vector<GraphicIRect>& GetOutputDamages();
70     sptr<Surface> GetFrameBufferSurface();
71     std::unique_ptr<FrameBufferEntry> GetFramebuffer();
72     void Dump(std::string &result) const;
73     void DumpFps(std::string &result, const std::string &arg) const;
74     void DumpHitchs(std::string &result, const std::string &arg) const;
75     void ClearFpsDump(std::string &result, const std::string &arg);
76     GSError ClearFrameBuffer();
77 
78     RosenError InitDevice();
79     /* only used for mock tests */
80     RosenError SetHdiOutputDevice(HdiDevice* device);
81     int32_t PreProcessLayersComp();
82     int32_t UpdateLayerCompType();
83     int32_t FlushScreen(std::vector<LayerPtr> &compClientLayers);
84     int32_t SetScreenClientInfo(const FrameBufferEntry &fbEntry);
85     int32_t Commit(sptr<SyncFence> &fbFence);
86     int32_t CommitAndGetReleaseFence(sptr<SyncFence> &fbFence, int32_t &skipState, bool &needFlush, bool isValidated);
87     int32_t UpdateInfosAfterCommit(sptr<SyncFence> fbFence);
88     int32_t ReleaseFramebuffer(const sptr<SyncFence>& releaseFence);
89     std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFence();
90     int32_t StartVSyncSampler(bool forceReSample = false);
91     void SetPendingMode(int64_t period, int64_t timestamp);
92     void ReleaseLayers(sptr<SyncFence>& releaseFence);
93     int32_t GetBufferCacheSize();
94 
95 private:
96     HdiDevice *device_ = nullptr;
97     sptr<VSyncSampler> sampler_ = nullptr;
98 
99     std::vector<sptr<SyncFence>> historicalPresentfences_;
100     sptr<SyncFence> thirdFrameAheadPresentFence_ = SyncFence::InvalidFence();
101     int32_t presentFenceIndex_ = 0;
102 
103     sptr<SurfaceBuffer> currFrameBuffer_ = nullptr;
104     sptr<SurfaceBuffer> lastFrameBuffer_ = nullptr;
105 
106     std::array<int64_t, COMPOSITION_RECORDS_NUM> compositionTimeRecords_ = {};
107     uint32_t compTimeRcdIndex_ = 0;
108     sptr<HdiFramebufferSurface> fbSurface_ = nullptr;
109     // layerId -- layer ptr
110     std::unordered_map<uint32_t, LayerPtr> layerIdMap_;
111     // surface unique id -- layer ptr
112     std::unordered_map<uint64_t, LayerPtr> surfaceIdMap_;
113     uint32_t screenId_;
114     std::vector<GraphicIRect> outputDamages_;
115     bool directClientCompositionEnabled_ = true;
116 
117     std::vector<sptr<SurfaceBuffer> > bufferCache_;
118     uint32_t bufferCacheCountMax_ = 0;
119     mutable std::mutex mutex_;
120 
121     std::vector<uint32_t> layersId_;
122     std::vector<sptr<SyncFence>> fences_;
123 
124     // DISPLAYENGINE
125     bool arsrPreEnabled_ = false;
126 
127     int32_t CreateLayerLocked(uint64_t surfaceId, const LayerInfoPtr &layerInfo);
128     void DeletePrevLayersLocked();
129     void ResetLayerStatusLocked();
130     void ReorderLayerInfoLocked(std::vector<LayerDumpInfo> &dumpLayerInfos) const;
131     void UpdatePrevLayerInfoLocked();
132     void ReleaseSurfaceBuffer(sptr<SyncFence>& releaseFence);
133     void RecordCompositionTime(int64_t timeStamp);
134     inline bool CheckFbSurface();
135     bool CheckAndUpdateClientBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index);
136 
137     // DISPLAY ENGINE
138     bool CheckIfDoArsrPre(const LayerInfoPtr &layerInfo);
139 
140     void ClearBufferCache();
141     std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFenceLocked();
142 };
143 } // namespace Rosen
144 } // namespace OHOS
145 
146 #endif // HDI_BACKEND_HDI_OUTPUT_H