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