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()62 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 100 private: 101 HdiDevice *device_ = nullptr; 102 sptr<VSyncSampler> sampler_ = nullptr; 103 104 std::vector<sptr<SyncFence>> historicalPresentfences_; 105 sptr<SyncFence> thirdFrameAheadPresentFence_ = SyncFence::InvalidFence(); 106 int32_t presentFenceIndex_ = 0; 107 108 sptr<SurfaceBuffer> currFrameBuffer_ = nullptr; 109 sptr<SurfaceBuffer> lastFrameBuffer_ = nullptr; 110 111 std::array<int64_t, COMPOSITION_RECORDS_NUM> compositionTimeRecords_ = {}; 112 uint32_t compTimeRcdIndex_ = 0; 113 sptr<HdiFramebufferSurface> fbSurface_ = nullptr; 114 // layerId -- layer ptr 115 std::unordered_map<uint32_t, LayerPtr> layerIdMap_; 116 // surface unique id -- layer ptr 117 std::unordered_map<uint64_t, LayerPtr> surfaceIdMap_; 118 uint32_t screenId_; 119 uint32_t layerCompCapacity_ = LAYER_COMPOSITION_CAPACITY_INVALID; 120 std::vector<GraphicIRect> outputDamages_; 121 bool directClientCompositionEnabled_ = true; 122 123 std::vector<sptr<SurfaceBuffer> > bufferCache_; 124 uint32_t bufferCacheCountMax_ = 0; 125 mutable std::mutex mutex_; 126 127 std::vector<uint32_t> layersId_; 128 std::vector<sptr<SyncFence>> fences_; 129 130 // DISPLAYENGINE 131 bool arsrPreEnabled_ = false; 132 133 int32_t CreateLayerLocked(uint64_t surfaceId, const LayerInfoPtr &layerInfo); 134 void DeletePrevLayersLocked(); 135 void ResetLayerStatusLocked(); 136 void ReorderLayerInfoLocked(std::vector<LayerDumpInfo> &dumpLayerInfos) const; 137 void UpdatePrevLayerInfoLocked(); 138 void ReleaseSurfaceBuffer(sptr<SyncFence>& releaseFence); 139 void RecordCompositionTime(int64_t timeStamp); 140 inline bool CheckFbSurface(); 141 bool CheckAndUpdateClientBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index); 142 143 // DISPLAY ENGINE 144 bool CheckIfDoArsrPre(const LayerInfoPtr &layerInfo); 145 146 void ClearBufferCache(); 147 std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFenceLocked(); 148 }; 149 } // namespace Rosen 150 } // namespace OHOS 151 152 #endif // HDI_BACKEND_HDI_OUTPUT_H