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 "hdi_backend.h" 24 #include "hgm_core.h" 25 #include "rs_main_thread.h" 26 #include "rs_vblank_idle_corrector.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 38 struct RefreshRateParam { 39 uint32_t rate = 0; 40 uint64_t frameTimestamp = 0; 41 int64_t actualTimestamp = 0; 42 uint64_t vsyncId = 0; 43 uint64_t constraintRelativeTime = 0; 44 bool isForceRefresh = false; 45 }; 46 47 class RSHardwareThread { 48 public: 49 static RSHardwareThread& Instance(); 50 void Start(); 51 void PostTask(const std::function<void()>& task); 52 void PostDelayTask(const std::function<void()>& task, int64_t delayTime); 53 void CommitAndReleaseLayers(OutputPtr output, const std::vector<LayerInfoPtr>& layers); 54 template<typename Task, typename Return = std::invoke_result_t<Task>> ScheduleTask(Task && task)55 std::future<Return> ScheduleTask(Task&& task) 56 { 57 auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task)); 58 PostTask([t(std::move(scheduledTask))]() { t->Run(); }); 59 return std::move(taskFuture); 60 } 61 uint32_t GetunExecuteTaskNum(); 62 void RefreshRateCounts(std::string& dumpString); 63 void ClearRefreshRateCounts(std::string& dumpString); 64 int GetHardwareTid() const; 65 GSError ClearFrameBuffers(OutputPtr output); 66 void OnScreenVBlankIdleCallback(ScreenId screenId, uint64_t timestamp); 67 private: 68 RSHardwareThread() = default; 69 ~RSHardwareThread() = default; 70 RSHardwareThread(const RSHardwareThread&); 71 RSHardwareThread(const RSHardwareThread&&); 72 RSHardwareThread& operator=(const RSHardwareThread&); 73 RSHardwareThread& operator=(const RSHardwareThread&&); 74 75 void OnPrepareComplete(sptr<Surface>& surface, const PrepareCompleteParam& param, void* data); 76 void Redraw(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers, uint32_t screenId); 77 void RedrawScreenRCD(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers); 78 void PerformSetActiveMode(OutputPtr output, uint64_t timestamp, uint64_t constraintRelativeTime); 79 void ExecuteSwitchRefreshRate(uint32_t rate); 80 void AddRefreshRateCount(); 81 RefreshRateParam GetRefreshRateParam(); 82 bool IsDelayRequired(OHOS::Rosen::HgmCore& hgmCore, RefreshRateParam param, bool hasGameScene); 83 void CalculateDelayTime(OHOS::Rosen::HgmCore& hgmCore, RefreshRateParam param, uint32_t currentRate, 84 int64_t currTime); 85 std::shared_ptr<RSSurfaceOhos> CreateFrameBufferSurfaceOhos(const sptr<Surface>& surface); 86 #ifdef RES_SCHED_ENABLE 87 void SubScribeSystemAbility(); 88 sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr; 89 #endif 90 #ifdef USE_VIDEO_PROCESSING_ENGINE 91 static GraphicColorGamut ComputeTargetColorGamut(const std::vector<LayerInfoPtr>& layers); 92 static GraphicPixelFormat ComputeTargetPixelFormat(const std::vector<LayerInfoPtr>& layers); 93 static bool ConvertColorGamutToSpaceType(const GraphicColorGamut& colorGamut, 94 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceInfo); 95 #endif 96 97 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 98 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 99 HdiBackend *hdiBackend_ = nullptr; 100 std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_; 101 UniFallbackCallback redrawCb_; 102 std::mutex mutex_; 103 std::atomic<uint32_t> unExecuteTaskNum_ = 0; 104 int hardwareTid_ = -1; 105 std::shared_ptr<RSSurfaceOhos> frameBufferSurfaceOhos_; 106 107 HgmRefreshRates hgmRefreshRates_ = HgmRefreshRates::SET_RATE_NULL; 108 RSVBlankIdleCorrector vblankIdleCorrector_; 109 110 std::map<uint32_t, uint64_t> refreshRateCounts_; 111 sptr<SyncFence> releaseFence_ = SyncFence::InvalidFence(); 112 int64_t delayTime_ = 0; 113 int64_t lastCommitTime_ = 0; 114 115 friend class RSUniRenderThread; 116 friend class RSUifirstManager; 117 }; 118 } 119 #endif // RS_HARDWARE_THREAD_H 120