1 /* 2 * Copyright (c) 2022-2023 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 RS_HARDWARE_THREAD_H 17 #define RS_HARDWARE_THREAD_H 18 19 #include <atomic> 20 #include <mutex> 21 22 #include "event_handler.h" 23 #include "feature/hyper_graphic_manager/hgm_hardware_utils.h" 24 #include "hdi_backend.h" 25 #include "hgm_core.h" 26 #include "pipeline/main_thread/rs_main_thread.h" 27 #ifdef RES_SCHED_ENABLE 28 #include "vsync_system_ability_listener.h" 29 #endif 30 31 namespace OHOS::Rosen { 32 using UniFallbackCallback = std::function<void(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers, 33 uint32_t screenId)>; 34 using OutputPtr = std::shared_ptr<HdiOutput>; 35 using LayerPtr = std::shared_ptr<HdiLayer>; 36 class ScheduledTask; 37 class RSHardwareThread { 38 public: 39 static RSHardwareThread& Instance(); 40 void Start(); 41 void PostTask(const std::function<void()>& task); 42 void PostSyncTask(const std::function<void()>& task); 43 void PostDelayTask(const std::function<void()>& task, int64_t delayTime); 44 void CommitAndReleaseLayers(OutputPtr output, const std::vector<LayerInfoPtr>& layers); 45 template<typename Task, typename Return = std::invoke_result_t<Task>> ScheduleTask(Task && task)46 std::future<Return> ScheduleTask(Task&& task) 47 { 48 auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task)); 49 #ifdef RS_ENABLE_GPU 50 PostTask([t(std::move(scheduledTask))]() { t->Run(); }); 51 #endif 52 return std::move(taskFuture); 53 } 54 uint32_t GetunExecuteTaskNum(); 55 void RefreshRateCounts(std::string& dumpString); 56 void ClearRefreshRateCounts(std::string& dumpString); 57 int GetHardwareTid() const; 58 GSError ClearFrameBuffers(OutputPtr output); 59 void OnScreenVBlankIdleCallback(ScreenId screenId, uint64_t timestamp); 60 void ClearRedrawGPUCompositionCache(const std::set<uint32_t>& bufferIds); 61 void DumpEventQueue(); 62 void PreAllocateProtectedBuffer(sptr<SurfaceBuffer> buffer, uint64_t screenId); 63 void ChangeLayersForActiveRectOutside(std::vector<LayerInfoPtr>& layers, ScreenId screenId); 64 void DumpVkImageInfo(std::string &dumpString); 65 GetDelayTime()66 int64_t GetDelayTime() 67 { 68 return delayTime_; 69 } 70 71 private: 72 RSHardwareThread() = default; 73 ~RSHardwareThread() = default; 74 RSHardwareThread(const RSHardwareThread&); 75 RSHardwareThread(const RSHardwareThread&&); 76 RSHardwareThread& operator=(const RSHardwareThread&); 77 RSHardwareThread& operator=(const RSHardwareThread&&); 78 79 void OnPrepareComplete(sptr<Surface>& surface, const PrepareCompleteParam& param, void* data); 80 void Redraw(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers, uint32_t screenId); 81 void RedrawScreenRCD(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers); 82 void AddRefreshRateCount(const OutputPtr& output); 83 void RecordTimestamp(const std::vector<LayerInfoPtr>& layers); 84 int64_t GetCurTimeCount(); 85 int32_t AdaptiveModeStatus(const OutputPtr &output); 86 87 RefreshRateParam GetRefreshRateParam(); 88 bool IsDelayRequired(OHOS::Rosen::HgmCore& hgmCore, RefreshRateParam param, 89 const OutputPtr& output, bool hasGameScene); 90 void CalculateDelayTime(OHOS::Rosen::HgmCore& hgmCore, RefreshRateParam param, uint32_t currentRate, 91 int64_t currTime); 92 std::shared_ptr<RSSurfaceOhos> CreateFrameBufferSurfaceOhos(const sptr<Surface>& surface); 93 void EndCheck(RSTimer timer); 94 bool IsDropDirtyFrame(const std::vector<LayerInfoPtr>& layerInfos, uint32_t screenId); 95 void ContextRegisterPostTask(); 96 #ifdef RES_SCHED_ENABLE 97 void SubScribeSystemAbility(); 98 sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr; 99 #endif 100 #ifdef USE_VIDEO_PROCESSING_ENGINE 101 static GraphicColorGamut ComputeTargetColorGamut(const std::vector<LayerInfoPtr>& layers); 102 static GraphicPixelFormat ComputeTargetPixelFormat(const std::vector<LayerInfoPtr>& layers); 103 static bool IsAllRedraw(const std::vector<LayerInfoPtr>& layers); 104 static bool ConvertColorGamutToSpaceType(const GraphicColorGamut& colorGamut, 105 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceInfo); 106 #endif 107 108 static GraphicColorGamut ComputeTargetColorGamut(const sptr<SurfaceBuffer> &buffer); 109 static GraphicPixelFormat ComputeTargetPixelFormat(const sptr<SurfaceBuffer> &buffer); 110 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 111 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 112 HdiBackend *hdiBackend_ = nullptr; 113 std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_; 114 UniFallbackCallback redrawCb_; 115 std::mutex mutex_; 116 std::atomic<uint32_t> unExecuteTaskNum_ = 0; 117 int hardwareTid_ = -1; 118 std::unordered_map<uint64_t, std::shared_ptr<RSSurfaceOhos>> frameBufferSurfaceOhosMap_; 119 120 std::map<uint32_t, uint64_t> refreshRateCounts_; 121 sptr<SyncFence> releaseFence_ = SyncFence::InvalidFence(); 122 int64_t delayTime_ = 0; 123 int64_t lastCommitTime_ = 0; 124 int64_t intervalTimePoints_ = 0; 125 int64_t lastActualTime_ = 0; 126 std::string GetSurfaceNameInLayers(const std::vector<LayerInfoPtr>& layers); 127 std::string GetSurfaceNameInLayersForTrace(const std::vector<LayerInfoPtr>& layers); 128 std::mutex preAllocMutex_; 129 std::mutex frameBufferSurfaceOhosMapMutex_; 130 std::mutex surfaceMutex_; 131 132 HgmHardwareUtils hgmHardwareUtils_; 133 134 friend class RSUniRenderThread; 135 friend class RSUifirstManager; 136 int hardwareCount_ = 0; 137 ExceptionCheck exceptionCheck_; 138 }; 139 } 140 141 namespace OHOS { 142 namespace AppExecFwk { 143 class RSHardwareDumper : public Dumper { 144 public: 145 void Dump(const std::string& message) override; 146 std::string GetTag() override; 147 void PrintDumpInfo(); 148 private: 149 std::string dumpInfo_; 150 }; 151 } // namespace AppExecFwk 152 } // namespace OHOS 153 #endif // RS_HARDWARE_THREAD_H 154