• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_MAIN_THREAD
17 #define RS_MAIN_THREAD
18 
19 #include <future>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 
25 #include "common/rs_thread_handler.h"
26 #include "common/rs_thread_looper.h"
27 #include "ipc_callbacks/iapplication_render_thread.h"
28 #include "pipeline/rs_context.h"
29 #include "platform/drawing/rs_vsync_client.h"
30 #include "refbase.h"
31 #include "vsync_receiver.h"
32 #include "vsync_distributor.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 class RSTransactionData;
37 
38 namespace Detail {
39 template<typename Task>
40 class ScheduledTask : public RefBase {
41 public:
Create(Task && task)42     static auto Create(Task&& task)
43     {
44         sptr<ScheduledTask<Task>> t(new ScheduledTask(std::forward<Task&&>(task)));
45         return std::make_pair(t, t->task_.get_future());
46     }
47 
Run()48     void Run()
49     {
50         task_();
51     }
52 
53 private:
ScheduledTask(Task && task)54     explicit ScheduledTask(Task&& task) : task_(std::move(task)) {}
55 
56     using Return = std::invoke_result_t<Task>;
57     std::packaged_task<Return()> task_;
58 };
59 } // namespace Detail
60 
61 class RSMainThread {
62 public:
63     static RSMainThread* Instance();
64 
65     void Init();
66     void Start();
67     void RecvRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData);
68     void RequestNextVSync();
69     void PostTask(RSTaskMessage::RSTask task);
70 
71     template<typename Task, typename Return = std::invoke_result_t<Task>>
ScheduleTask(Task && task)72     std::future<Return> ScheduleTask(Task&& task)
73     {
74         auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task));
75         PostTask([t(std::move(scheduledTask))]() { t->Run(); });
76         return std::move(taskFuture);
77     }
78 
GetContext()79     RSContext& GetContext()
80     {
81         return context_;
82     }
Id()83     std::thread::id Id() const
84     {
85         return mainThreadId_;
86     }
87     void RegisterApplicationRenderThread(uint32_t pid, sptr<IApplicationRenderThread> app);
88     void UnregisterApplicationRenderThread(sptr<IApplicationRenderThread> app);
89 
90     sptr<VSyncDistributor> rsVSyncDistributor_;
91 private:
92     RSMainThread();
93     ~RSMainThread() noexcept;
94     RSMainThread(const RSMainThread&) = delete;
95     RSMainThread(const RSMainThread&&) = delete;
96     RSMainThread& operator=(const RSMainThread&) = delete;
97     RSMainThread& operator=(const RSMainThread&&) = delete;
98 
99     void OnVsync(uint64_t timestamp, void *data);
100     void ProcessCommand();
101     void Animate(uint64_t timestamp);
102     void Render();
103     void SendCommands();
104 
105     std::mutex transitionDataMutex_;
106     std::unique_ptr<RSThreadLooper> threadLooper_ = nullptr;
107     std::unique_ptr<RSThreadHandler> threadHandler_ = nullptr;
108     RSTaskHandle taskHandle_ = nullptr;
109     RSTaskMessage::RSTask mainLoop_;
110     std::unique_ptr<RSVsyncClient> vsyncClient_ = nullptr;
111     std::queue<std::unique_ptr<RSTransactionData>> cacheCommandQueue_;
112     std::queue<std::unique_ptr<RSTransactionData>> effectCommandQueue_;
113 
114     uint64_t timestamp_ = 0;
115     std::unordered_map<uint32_t, sptr<IApplicationRenderThread>> applicationRenderThreadMap_;
116 
117     RSContext context_;
118     std::thread::id mainThreadId_;
119     std::shared_ptr<VSyncReceiver> receiver_ = nullptr;
120 };
121 } // namespace Rosen
122 } // namespace OHOS
123 #endif // RS_MAIN_THREAD
124