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