• 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 <mutex>
20 
21 #include "event_handler.h"
22 #include "hdi_backend.h"
23 #include "rs_main_thread.h"
24 
25 namespace OHOS::Rosen {
26 using UniFallbackCallback = std::function<void(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers,
27     uint32_t screenId)>;
28 using OutputPtr = std::shared_ptr<HdiOutput>;
29 using LayerPtr = std::shared_ptr<HdiLayer>;
30 class ScheduledTask;
31 class RSHardwareThread {
32 public:
33     static RSHardwareThread& Instance();
34     void Start();
35     void PostTask(const std::function<void()>& task);
36     void CommitAndReleaseLayers(OutputPtr output, const std::vector<LayerInfoPtr>& layers);
37     void ReleaseBuffer(sptr<SurfaceBuffer> buffer, sptr<SyncFence> releaseFence, sptr<IConsumerSurface> cSurface);
38     void CleanRenderFrame(uint64_t id);
39     template<typename Task, typename Return = std::invoke_result_t<Task>>
ScheduleTask(Task && task)40     std::future<Return> ScheduleTask(Task&& task)
41     {
42         auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task));
43         PostTask([t(std::move(scheduledTask))]() { t->Run(); });
44         return std::move(taskFuture);
45     }
46 private:
47     RSHardwareThread() = default;
48     ~RSHardwareThread() = default;
49     RSHardwareThread(const RSHardwareThread&);
50     RSHardwareThread(const RSHardwareThread&&);
51     RSHardwareThread& operator=(const RSHardwareThread&);
52     RSHardwareThread& operator=(const RSHardwareThread&&);
53 
54     void OnPrepareComplete(sptr<Surface>& surface, const PrepareCompleteParam& param, void* data);
55     void Redraw(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers, uint32_t screenId);
56     void ReleaseLayers(OutputPtr output, const std::unordered_map<uint32_t, LayerPtr>& layerMap);
57     void LayerPresentTimestamp(const LayerInfoPtr& layer, const sptr<IConsumerSurface>& surface) const;
58     void PerformSetActiveMode();
59 
60     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
61     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
62     HdiBackend *hdiBackend_ = nullptr;
63     std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_;
64     UniFallbackCallback redrawCb_;
65     std::mutex mutex_;
66 
67     bool lockRefreshRateOnce_ = false;
68     HgmRefreshRates hgmRefreshRates_;
69     HgmRefreshRateModes hgmRefreshRateModes_;
70 };
71 }
72 #endif // RS_HARDWARE_THREAD_H
73