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 <list> 21 #include <stdint.h> 22 #include <vector> 23 #include <unordered_map> 24 25 #include "graphic_error.h" 26 #include "surface_type.h" 27 #include "hdi_layer.h" 28 #include "hdi_framebuffer_surface.h" 29 #include "hdi_screen.h" 30 #include "vsync_sampler.h" 31 32 namespace OHOS { 33 namespace Rosen { 34 35 using LayerPtr = std::shared_ptr<HdiLayer>; 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 // only used when composer_host dead ResetDevice()56 void ResetDevice() 57 { 58 device_ = nullptr; 59 } IsDeviceValid()60 bool IsDeviceValid() const 61 { 62 return device_ != nullptr; 63 } 64 /* for RS end */ 65 66 static std::shared_ptr<HdiOutput> CreateHdiOutput(uint32_t screenId); 67 RosenError Init(); 68 void GetLayerInfos(std::vector<LayerInfoPtr>& layerInfos); 69 void GetComposeClientLayers(std::vector<LayerPtr>& clientLayers); 70 const std::vector<GraphicIRect>& GetOutputDamages(); 71 sptr<Surface> GetFrameBufferSurface(); 72 std::unique_ptr<FrameBufferEntry> GetFramebuffer(); 73 void Dump(std::string &result) const; 74 void DumpCurrentFrameLayers() const; 75 void DumpFps(std::string &result, const std::string &arg) const; 76 void DumpHitchs(std::string &result, const std::string &arg) const; 77 void ClearFpsDump(std::string &result, const std::string &arg); 78 GSError ClearFrameBuffer(); 79 80 RosenError InitDevice(); 81 /* only used for mock tests */ 82 RosenError SetHdiOutputDevice(HdiDevice* device); 83 int32_t PreProcessLayersComp(); 84 int32_t UpdateLayerCompType(); 85 int32_t FlushScreen(std::vector<LayerPtr> &compClientLayers); 86 int32_t SetScreenClientInfo(const FrameBufferEntry &fbEntry); 87 int32_t Commit(sptr<SyncFence> &fbFence); 88 int32_t CommitAndGetReleaseFence(sptr<SyncFence> &fbFence, int32_t &skipState, bool &needFlush, bool isValidated); 89 int32_t UpdateInfosAfterCommit(sptr<SyncFence> fbFence); 90 int32_t ReleaseFramebuffer(const sptr<SyncFence>& releaseFence); 91 std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFence(); 92 int32_t StartVSyncSampler(bool forceReSample = false); 93 void SetPendingMode(int64_t period, int64_t timestamp); 94 void ReleaseLayers(sptr<SyncFence>& releaseFence); 95 int32_t GetBufferCacheSize(); 96 void SetVsyncSamplerEnabled(bool enabled); 97 bool GetVsyncSamplerEnabled(); SetProtectedFrameBufferState(bool state)98 void SetProtectedFrameBufferState(bool state) 99 { 100 isProtectedBufferAllocated_.store(state); 101 } GetProtectedFrameBufferState()102 bool GetProtectedFrameBufferState() 103 { 104 return isProtectedBufferAllocated_.load(); 105 } 106 void CleanLayerBufferBySurfaceId(uint64_t surfaceId); 107 108 void SetActiveRectSwitchStatus(bool flag); 109 110 void ANCOTransactionOnComplete(const LayerInfoPtr& layerInfo, const sptr<SyncFence>& previousReleaseFence); 111 SetMaskLayer(LayerPtr maskLayer)112 void SetMaskLayer(LayerPtr maskLayer) 113 { 114 maskLayer_ = maskLayer; 115 } 116 117 private: 118 HdiDevice *device_ = nullptr; 119 sptr<VSyncSampler> sampler_ = nullptr; 120 121 std::vector<sptr<SyncFence>> historicalPresentfences_; 122 sptr<SyncFence> thirdFrameAheadPresentFence_ = SyncFence::InvalidFence(); 123 int32_t presentFenceIndex_ = 0; 124 125 sptr<SurfaceBuffer> currFrameBuffer_ = nullptr; 126 sptr<SurfaceBuffer> lastFrameBuffer_ = nullptr; 127 128 std::array<int64_t, COMPOSITION_RECORDS_NUM> compositionTimeRecords_ = {}; 129 uint32_t compTimeRcdIndex_ = 0; 130 sptr<HdiFramebufferSurface> fbSurface_ = nullptr; 131 std::list<LayerPtr> layersTobeRelease_; 132 133 // layerId -- layer ptr 134 std::unordered_map<uint32_t, LayerPtr> layerIdMap_; 135 // surface unique id -- layer ptr 136 std::unordered_map<uint64_t, LayerPtr> surfaceIdMap_; 137 // solidLayer unique id -- layer ptr 138 std::unordered_map<uint64_t, LayerPtr> solidSurfaceIdMap_; 139 uint32_t screenId_; 140 std::vector<GraphicIRect> outputDamages_; 141 bool directClientCompositionEnabled_ = true; 142 143 std::vector<sptr<SurfaceBuffer> > bufferCache_; 144 uint32_t bufferCacheCountMax_ = 0; 145 mutable std::mutex mutex_; 146 147 std::vector<uint32_t> layersId_; 148 std::vector<sptr<SyncFence>> fences_; 149 150 // DISPLAYENGINE 151 bool arsrPreEnabled_ = false; 152 bool arsrPreEnabledForVm_ = false; 153 std::string vmArsrWhiteList_ = ""; 154 // Protected framebuffer is allocated in advance 155 std::atomic<bool> isProtectedBufferAllocated_ = false; 156 157 LayerPtr maskLayer_ = nullptr; 158 159 int32_t CreateLayerLocked(uint64_t surfaceId, const LayerInfoPtr &layerInfo); 160 void DeletePrevLayersLocked(); 161 void ResetLayerStatusLocked(); 162 void ReorderLayerInfoLocked(std::vector<LayerDumpInfo> &dumpLayerInfos) const; 163 void UpdatePrevLayerInfoLocked(); 164 void ReleaseSurfaceBuffer(sptr<SyncFence>& releaseFence); 165 void RecordCompositionTime(int64_t timeStamp); 166 inline bool CheckFbSurface(); 167 bool CheckAndUpdateClientBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index); 168 169 // DISPLAY ENGINE 170 bool CheckIfDoArsrPre(const LayerInfoPtr &layerInfo); 171 bool CheckIfDoArsrPreForVm(const LayerInfoPtr &layerInfo); 172 bool CheckSupportArsrPreMetadata(); 173 bool CheckSupportCopybitMetadata(); 174 175 void ClearBufferCache(); 176 std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFenceLocked(); 177 178 bool isActiveRectSwitching_ = false; 179 void DirtyRegions(uint32_t solidLayerCount, const LayerInfoPtr &layerInfo); 180 }; 181 } // namespace Rosen 182 } // namespace OHOS 183 184 #endif // HDI_BACKEND_HDI_OUTPUT_H