• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "surface_type.h"
25 #include "hdi_layer.h"
26 #include "hdi_framebuffer_surface.h"
27 #include "hdi_screen.h"
28 #include "vsync_sampler.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 
33 using LayerPtr = std::shared_ptr<HdiLayer>;
34 static constexpr uint32_t LAYER_COMPOSITION_CAPACITY_INVALID = 0;
35 
36 // dump layer
37 struct LayerDumpInfo {
38     uint64_t surfaceId;
39     LayerPtr layer;
40 };
41 
42 class HdiOutput {
43 public:
44     HdiOutput(uint32_t screenId);
45     virtual ~HdiOutput();
46 
47     static constexpr uint32_t COMPOSITION_RECORDS_NUM = 128;
48 
49     /* for RS begin */
50     void SetLayerInfo(const std::vector<LayerInfoPtr> &layerInfos);
51     void SetOutputDamages(const std::vector<GraphicIRect> &outputDamages);
52     uint32_t GetScreenId() const;
53     void SetLayerCompCapacity(uint32_t layerCompositionCapacity);
54     uint32_t GetLayerCompCapacity() const;
55     /* for RS end */
56 
57     static std::shared_ptr<HdiOutput> CreateHdiOutput(uint32_t screenId);
58     RosenError Init();
59     const std::unordered_map<uint32_t, LayerPtr>& GetLayers();
60     const std::vector<GraphicIRect>& GetOutputDamages();
61     sptr<Surface> GetFrameBufferSurface();
62     std::unique_ptr<FrameBufferEntry> GetFramebuffer();
63     void Dump(std::string &result) const;
64     void DumpFps(std::string &result, const std::string &arg) const;
65     void ClearFpsDump(std::string &result, const std::string &arg);
66     void SetDirectClientCompEnableStatus(bool enableStatus);
67     bool GetDirectClientCompEnableStatus() const;
68 
69     RosenError InitDevice();
70     /* only used for mock tests */
71     RosenError SetHdiOutputDevice(HdiDevice* device);
72     int32_t PreProcessLayersComp(bool &needFlush);
73     int32_t UpdateLayerCompType();
74     int32_t FlushScreen(std::vector<LayerPtr> &compClientLayers);
75     int32_t SetScreenClientInfo(const FrameBufferEntry &fbEntry);
76     int32_t Commit(sptr<SyncFence> &fbFence);
77     int32_t UpdateInfosAfterCommit(sptr<SyncFence> fbFence);
78     int32_t ReleaseFramebuffer(const sptr<SyncFence>& releaseFence);
79     std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFence();
80 
81 private:
82     HdiDevice *device_ = nullptr;
83     sptr<VSyncSampler> sampler_ = nullptr;
84     sptr<SyncFence> lastPresentFence_ = SyncFence::INVALID_FENCE;
85     sptr<SurfaceBuffer> currFrameBuffer_ = nullptr;
86     sptr<SurfaceBuffer> lastFrameBuffer_ = nullptr;
87 
88     std::array<int64_t, COMPOSITION_RECORDS_NUM> compositionTimeRecords_ = {};
89     uint32_t compTimeRcdIndex_ = 0;
90     sptr<HdiFramebufferSurface> fbSurface_ = nullptr;
91     // layerId -- layer ptr
92     std::unordered_map<uint32_t, LayerPtr> layerIdMap_;
93     // surface unique id -- layer ptr
94     std::unordered_map<uint64_t, LayerPtr> surfaceIdMap_;
95     uint32_t screenId_;
96     uint32_t layerCompCapacity_ = LAYER_COMPOSITION_CAPACITY_INVALID;
97     std::vector<GraphicIRect> outputDamages_;
98     bool directClientCompositionEnabled_ = true;
99 
100     std::vector<sptr<SurfaceBuffer> > bufferCache_;
101     uint32_t bufferCacheCountMax_ = 0;
102     uint32_t bufferCacheIndex_ = 0;
103 
104     int32_t CreateLayer(uint64_t surfaceId, const LayerInfoPtr &layerInfo);
105     void DeletePrevLayers();
106     void ResetLayerStatus();
107     void ReorderLayerInfo(std::vector<LayerDumpInfo> &dumpLayerInfos) const;
108     void UpdatePrevLayerInfo();
109     void RecordCompositionTime(int64_t timeStamp);
110     inline bool CheckFbSurface();
111     bool CheckAndUpdateClientBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index);
112 };
113 } // namespace Rosen
114 } // namespace OHOS
115 
116 #endif // HDI_BACKEND_HDI_OUTPUT_H