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 void SetVsyncSamplerEnabled(bool enabled); 95 bool GetVsyncSamplerEnabled(); SetProtectedFrameBufferState(bool state)96 void SetProtectedFrameBufferState(bool state) 97 { 98 isProtectedBufferAllocated_.store(state); 99 } GetProtectedFrameBufferState()100 bool GetProtectedFrameBufferState() 101 { 102 return isProtectedBufferAllocated_.load(); 103 } 104 void CleanLayerBufferBySurfaceId(uint64_t surfaceId); 105 106 private: 107 HdiDevice *device_ = nullptr; 108 sptr<VSyncSampler> sampler_ = nullptr; 109 110 std::vector<sptr<SyncFence>> historicalPresentfences_; 111 sptr<SyncFence> thirdFrameAheadPresentFence_ = SyncFence::InvalidFence(); 112 int32_t presentFenceIndex_ = 0; 113 114 sptr<SurfaceBuffer> currFrameBuffer_ = nullptr; 115 sptr<SurfaceBuffer> lastFrameBuffer_ = nullptr; 116 117 std::array<int64_t, COMPOSITION_RECORDS_NUM> compositionTimeRecords_ = {}; 118 uint32_t compTimeRcdIndex_ = 0; 119 sptr<HdiFramebufferSurface> fbSurface_ = nullptr; 120 // layerId -- layer ptr 121 std::unordered_map<uint32_t, LayerPtr> layerIdMap_; 122 // surface unique id -- layer ptr 123 std::unordered_map<uint64_t, LayerPtr> surfaceIdMap_; 124 uint32_t screenId_; 125 std::vector<GraphicIRect> outputDamages_; 126 bool directClientCompositionEnabled_ = true; 127 128 std::vector<sptr<SurfaceBuffer> > bufferCache_; 129 uint32_t bufferCacheCountMax_ = 0; 130 mutable std::mutex mutex_; 131 132 std::vector<uint32_t> layersId_; 133 std::vector<sptr<SyncFence>> fences_; 134 135 // DISPLAYENGINE 136 bool arsrPreEnabled_ = false; 137 bool arsrPreEnabledForVm_ = false; 138 std::string vmArsrWhiteList_ = ""; 139 // Protected framebuffer is allocated in advance 140 std::atomic<bool> isProtectedBufferAllocated_ = false; 141 142 int32_t CreateLayerLocked(uint64_t surfaceId, const LayerInfoPtr &layerInfo); 143 void DeletePrevLayersLocked(); 144 void ResetLayerStatusLocked(); 145 void ReorderLayerInfoLocked(std::vector<LayerDumpInfo> &dumpLayerInfos) const; 146 void UpdatePrevLayerInfoLocked(); 147 void ReleaseSurfaceBuffer(sptr<SyncFence>& releaseFence); 148 void RecordCompositionTime(int64_t timeStamp); 149 inline bool CheckFbSurface(); 150 bool CheckAndUpdateClientBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index); 151 152 // DISPLAY ENGINE 153 bool CheckIfDoArsrPre(const LayerInfoPtr &layerInfo); 154 bool CheckIfDoArsrPreForVm(const LayerInfoPtr &layerInfo); 155 bool CheckSupportArsrPreMetadata(); 156 157 void ClearBufferCache(); 158 std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFenceLocked(); 159 }; 160 } // namespace Rosen 161 } // namespace OHOS 162 163 #endif // HDI_BACKEND_HDI_OUTPUT_H