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 = HdiLayer::FRAME_RECORDS_NUM; 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 // 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 const std::unordered_map<uint32_t, LayerPtr>& GetLayers(); 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 ClearFpsDump(std::string &result, const std::string &arg); 75 void SetDirectClientCompEnableStatus(bool enableStatus); 76 bool GetDirectClientCompEnableStatus() const; 77 GSError ClearFrameBuffer(); 78 79 RosenError InitDevice(); 80 /* only used for mock tests */ 81 RosenError SetHdiOutputDevice(HdiDevice* device); 82 int32_t PreProcessLayersComp(bool &needFlush); 83 int32_t UpdateLayerCompType(); 84 int32_t FlushScreen(std::vector<LayerPtr> &compClientLayers); 85 int32_t SetScreenClientInfo(const FrameBufferEntry &fbEntry); 86 int32_t Commit(sptr<SyncFence> &fbFence); 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 93 private: 94 HdiDevice *device_ = nullptr; 95 sptr<VSyncSampler> sampler_ = nullptr; 96 sptr<SyncFence> lastPresentFence_ = SyncFence::INVALID_FENCE; 97 sptr<SurfaceBuffer> currFrameBuffer_ = nullptr; 98 sptr<SurfaceBuffer> lastFrameBuffer_ = nullptr; 99 100 std::array<int64_t, COMPOSITION_RECORDS_NUM> compositionTimeRecords_ = {}; 101 uint32_t compTimeRcdIndex_ = 0; 102 sptr<HdiFramebufferSurface> fbSurface_ = nullptr; 103 // layerId -- layer ptr 104 std::unordered_map<uint32_t, LayerPtr> layerIdMap_; 105 // surface unique id -- layer ptr 106 std::unordered_map<uint64_t, LayerPtr> surfaceIdMap_; 107 uint32_t screenId_; 108 uint32_t layerCompCapacity_ = LAYER_COMPOSITION_CAPACITY_INVALID; 109 std::vector<GraphicIRect> outputDamages_; 110 bool directClientCompositionEnabled_ = true; 111 112 std::vector<sptr<SurfaceBuffer> > bufferCache_; 113 uint32_t bufferCacheCountMax_ = 0; 114 115 int32_t CreateLayer(uint64_t surfaceId, const LayerInfoPtr &layerInfo); 116 void DeletePrevLayers(); 117 void ResetLayerStatus(); 118 void ReorderLayerInfo(std::vector<LayerDumpInfo> &dumpLayerInfos) const; 119 void UpdatePrevLayerInfo(); 120 void RecordCompositionTime(int64_t timeStamp); 121 inline bool CheckFbSurface(); 122 bool CheckAndUpdateClientBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index); 123 static void SetBufferColorSpace(sptr<SurfaceBuffer>& buffer, const std::vector<LayerPtr>& layers); 124 }; 125 } // namespace Rosen 126 } // namespace OHOS 127 128 #endif // HDI_BACKEND_HDI_OUTPUT_H