• 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 #include "rs_thread_handler_generic.h"
17 
18 namespace OHOS {
19 namespace Rosen {
Create()20 std::unique_ptr<RSThreadHandler> RSThreadHandler::Create()
21 {
22     return std::make_unique<RSThreadHandlerGeneric>();
23 }
24 
RSThreadHandlerGeneric()25 RSThreadHandlerGeneric::RSThreadHandlerGeneric()
26 {
27     looper_ = ThreadLooperImpl::GetThreadInstance();
28 }
29 
30 class GenericMessageHandler final : public ThreadLooperMessage {
31 public:
GenericMessageHandler(const RSTaskMessage::RSTask task)32     explicit GenericMessageHandler(const RSTaskMessage::RSTask task) : task_(task) {}
33 
34     ~GenericMessageHandler() override = default;
35 
Process(int param) const36     void Process(int param) const override
37     {
38         task_();
39     }
40 
41 private:
42     RSTaskMessage::RSTask task_ = nullptr;
43 };
44 
45 class GenericThreadMessage final : public RSTaskMessage {
46 public:
GenericThreadMessage(const RSTaskMessage::RSTask task)47     explicit GenericThreadMessage(const RSTaskMessage::RSTask task) : messageHandler_(new GenericMessageHandler(task))
48     {}
49     ~GenericThreadMessage() = default;
50 
GetMessageHandler() const51     const std::shared_ptr<GenericMessageHandler>& GetMessageHandler() const
52     {
53         return messageHandler_;
54     }
55 
56 private:
57     std::shared_ptr<GenericMessageHandler> messageHandler_;
58 };
59 
CreateTask(const RSTaskMessage::RSTask task) const60 RSTaskHandle RSThreadHandlerGeneric::CreateTask(const RSTaskMessage::RSTask task) const
61 {
62     return std::make_shared<GenericThreadMessage>(task);
63 }
64 
PostTask(const RSTaskHandle taskHandle,int param)65 void RSThreadHandlerGeneric::PostTask(const RSTaskHandle taskHandle, int param)
66 {
67     if (!looper_) {
68         return;
69     }
70     auto* msg = static_cast<GenericThreadMessage*>(taskHandle.get());
71     if (msg != nullptr) {
72         looper_->PostMessage(msg->GetMessageHandler(), param);
73     }
74 }
75 
PostTaskDelay(const RSTaskHandle taskHandle,int64_t nsecs,int param)76 void RSThreadHandlerGeneric::PostTaskDelay(const RSTaskHandle taskHandle, int64_t nsecs, int param)
77 {
78     if (!looper_) {
79         return;
80     }
81     auto* msg = static_cast<GenericThreadMessage*>(taskHandle.get());
82     if (msg != nullptr) {
83         looper_->PostMessage(nsecs, msg->GetMessageHandler(), param);
84     }
85 }
86 
CancelTask(RSTaskHandle taskHandle)87 void RSThreadHandlerGeneric::CancelTask(RSTaskHandle taskHandle)
88 {
89     if (!looper_) {
90         return;
91     }
92     auto* msg = static_cast<GenericThreadMessage*>(taskHandle.get());
93     if (msg != nullptr) {
94         looper_->RemoveMessages(msg->GetMessageHandler());
95     }
96 }
97 
StaticCreateTask(const RSTaskMessage::RSTask task)98 RSTaskHandle RSThreadHandler::StaticCreateTask(const RSTaskMessage::RSTask task)
99 {
100     return std::make_shared<GenericThreadMessage>(task);
101 }
102 } // namespace Rosen
103 } // namespace OHOS
104