• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pipeline/main_thread/rs_main_thread.h"
26 #include "feature/hyper_graphic_manager/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     uint64_t fastComposeTimeStampDiff = 0;
46 };
47 
48 class RSHardwareThread {
49 public:
50     static RSHardwareThread& Instance();
51     void Start();
52     void PostTask(const std::function<void()>& task);
53     void PostSyncTask(const std::function<void()>& task);
54     void PostDelayTask(const std::function<void()>& task, int64_t delayTime);
55     void CommitAndReleaseLayers(OutputPtr output, const std::vector<LayerInfoPtr>& layers);
56     template<typename Task, typename Return = std::invoke_result_t<Task>>
ScheduleTask(Task && task)57     std::future<Return> ScheduleTask(Task&& task)
58     {
59         auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task));
60 #ifdef RS_ENABLE_GPU
61         PostTask([t(std::move(scheduledTask))]() { t->Run(); });
62 #endif
63         return std::move(taskFuture);
64     }
65     uint32_t GetunExecuteTaskNum();
66     void RefreshRateCounts(std::string& dumpString);
67     void ClearRefreshRateCounts(std::string& dumpString);
68     int GetHardwareTid() const;
69     GSError ClearFrameBuffers(OutputPtr output);
70     void OnScreenVBlankIdleCallback(ScreenId screenId, uint64_t timestamp);
71     void ClearRedrawGPUCompositionCache(const std::set<uint32_t>& bufferIds);
72     void DumpEventQueue();
73     void PreAllocateProtectedBuffer(sptr<SurfaceBuffer> buffer, uint64_t screenId);
74     void ChangeLayersForActiveRectOutside(std::vector<LayerInfoPtr>& layers, ScreenId screenId);
75     void DumpVkImageInfo(std::string &dumpString);
76 private:
77     RSHardwareThread() = default;
78     ~RSHardwareThread() = default;
79     RSHardwareThread(const RSHardwareThread&);
80     RSHardwareThread(const RSHardwareThread&&);
81     RSHardwareThread& operator=(const RSHardwareThread&);
82     RSHardwareThread& operator=(const RSHardwareThread&&);
83 
84     void OnPrepareComplete(sptr<Surface>& surface, const PrepareCompleteParam& param, void* data);
85     void Redraw(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers, uint32_t screenId);
86     void RedrawScreenRCD(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers);
87     void PerformSetActiveMode(OutputPtr output, uint64_t timestamp, uint64_t constraintRelativeTime);
88     void ExecuteSwitchRefreshRate(const OutputPtr& output, uint32_t refreshRate);
89     void ChangeDssRefreshRate(ScreenId screenId, uint32_t refreshRate, bool followPipline);
90     void AddRefreshRateCount(const OutputPtr& output);
91     void RecordTimestamp(const std::vector<LayerInfoPtr>& layers);
92     int64_t GetCurTimeCount();
93     int32_t AdaptiveModeStatus(const OutputPtr &output);
94 
95     RefreshRateParam GetRefreshRateParam();
96     bool IsDelayRequired(OHOS::Rosen::HgmCore& hgmCore, RefreshRateParam param,
97         const OutputPtr& output, bool hasGameScene);
98     void CalculateDelayTime(OHOS::Rosen::HgmCore& hgmCore, RefreshRateParam param, uint32_t currentRate,
99         int64_t currTime);
100     std::shared_ptr<RSSurfaceOhos> CreateFrameBufferSurfaceOhos(const sptr<Surface>& surface);
101 #ifdef RES_SCHED_ENABLE
102     void SubScribeSystemAbility();
103     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
104 #endif
105 #ifdef USE_VIDEO_PROCESSING_ENGINE
106     static GraphicColorGamut ComputeTargetColorGamut(const std::vector<LayerInfoPtr>& layers);
107     static GraphicPixelFormat ComputeTargetPixelFormat(const std::vector<LayerInfoPtr>& layers);
108     static bool ConvertColorGamutToSpaceType(const GraphicColorGamut& colorGamut,
109         HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceInfo);
110 #endif
111 
112     static GraphicColorGamut ComputeTargetColorGamut(const sptr<SurfaceBuffer> &buffer);
113     static GraphicPixelFormat ComputeTargetPixelFormat(const sptr<SurfaceBuffer> &buffer);
114     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
115     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
116     HdiBackend *hdiBackend_ = nullptr;
117     std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_;
118     UniFallbackCallback redrawCb_;
119     std::mutex mutex_;
120     std::atomic<uint32_t> unExecuteTaskNum_ = 0;
121     int hardwareTid_ = -1;
122     std::unordered_map<uint64_t, std::shared_ptr<RSSurfaceOhos>> frameBufferSurfaceOhosMap_;
123 
124     HgmRefreshRates hgmRefreshRates_ = HgmRefreshRates::SET_RATE_NULL;
125     RSVBlankIdleCorrector vblankIdleCorrector_;
126 
127     std::map<uint32_t, uint64_t> refreshRateCounts_;
128     sptr<SyncFence> releaseFence_ = SyncFence::InvalidFence();
129     int64_t delayTime_ = 0;
130     int64_t lastCommitTime_ = 0;
131     int64_t intervalTimePoints_ = 0;
132     bool isLastAdaptive_ = false;
133     std::string GetSurfaceNameInLayers(const std::vector<LayerInfoPtr>& layers);
134     std::mutex preAllocMutex_;
135     std::mutex frameBufferSurfaceOhosMapMutex_;
136     std::mutex surfaceMutex_;
137 
138     bool needRetrySetRate_ = false;
139 
140     std::unordered_map<ScreenId, OutputPtr> outputMap_;
141     RefreshRateParam refreshRateParam_;
142 
143     friend class RSUniRenderThread;
144     friend class RSUifirstManager;
145 };
146 }
147 
148 namespace OHOS {
149 namespace AppExecFwk {
150 class RSHardwareDumper : public Dumper {
151 public:
152     void Dump(const std::string& message) override;
153     std::string GetTag() override;
154     void PrintDumpInfo();
155 private:
156     std::string dumpInfo_;
157 };
158 } // namespace AppExecFwk
159 } // namespace OHOS
160 #endif // RS_HARDWARE_THREAD_H
161