• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_MODIFIERS_DRAW_THREAD_H
17 #define RS_MODIFIERS_DRAW_THREAD_H
18 
19 #include <algorithm>
20 #include <future>
21 #include <mutex>
22 #include <shared_mutex>
23 #include <vector>
24 
25 #include "common/rs_common_def.h"
26 #include "event_handler.h"
27 #include "refbase.h"
28 #include "transaction/rs_irender_client.h"
29 #include "transaction/rs_transaction_data.h"
30 
31 #ifdef ACCESSIBILITY_ENABLE
32 #include "accessibility_config.h"
33 #ifdef ENABLE_IPC_SECURITY
34 #include "ipc_skeleton.h"
35 #include "accesstoken_kit.h"
36 #endif
37 #endif
38 
39 namespace OHOS {
40 namespace Rosen {
41 
42 namespace Detail {
43 template<typename Task>
44 class ScheduledTask : public RefBase {
45 public:
Create(Task && task)46     static auto Create(Task&& task)
47     {
48         sptr<ScheduledTask<Task>> scheduledTask(new ScheduledTask(std::forward<Task&&>(task)));
49         return std::make_pair(scheduledTask, scheduledTask->task_.get_future());
50     }
51 
Run()52     void Run()
53     {
54         task_();
55     }
56 
57 private:
ScheduledTask(Task && task)58     explicit ScheduledTask(Task&& task) : task_(std::move(task)) {}
59     ~ScheduledTask() override = default;
60 
61     using Return = std::invoke_result_t<Task>;
62     std::packaged_task<Return()> task_;
63 };
64 
65 #ifdef ACCESSIBILITY_ENABLE
66 class HighContrastObserver : public AccessibilityConfig::AccessibilityConfigObserver {
67 public:
HighContrastObserver(bool & highContrast)68     HighContrastObserver(bool& highContrast) : highContrastEnabled_(highContrast) {}
69 
OnConfigChanged(const AccessibilityConfig::CONFIG_ID id,const AccessibilityConfig::ConfigValue & value)70     void OnConfigChanged(const AccessibilityConfig::CONFIG_ID id, const AccessibilityConfig::ConfigValue& value)
71     {
72         // Non-system app, the first value is incorrect.
73         if (!first_ || IsSystemApp()) {
74             highContrastEnabled_ = value.highContrastText;
75         }
76         first_ = false;
77     }
78 
IsSystemApp()79     bool IsSystemApp()
80     {
81         if (!isSystemApp_.has_value()) {
82 #ifdef ENABLE_IPC_SECURITY
83             uint64_t tokenId = OHOS::IPCSkeleton::GetCallingFullTokenID();
84             isSystemApp_ = Security::AccessToken::AccessTokenKit::IsSystemAppByFullTokenID(tokenId);
85 #else
86             isSystemApp_ = false;
87 #endif
88         }
89         return isSystemApp_.value();
90     }
91 
92 private:
93     bool& highContrastEnabled_;
94     bool first_ = true;
95     std::optional<bool> isSystemApp_;
96 };
97 #endif
98 } // namespace Detail
99 
100 class TransactionDataHolder {
101 public:
102     TransactionDataHolder(const TransactionDataHolder&) = delete;
103     TransactionDataHolder& operator=(const TransactionDataHolder&) = delete;
TransactionDataHolder(std::unique_ptr<RSTransactionData> && transactionData)104     explicit TransactionDataHolder(std::unique_ptr<RSTransactionData>&& transactionData)
105         : data_(std::move(transactionData)) {}
106 private:
107     std::unique_ptr<RSTransactionData> data_;
108 };
109 
110 class RSB_EXPORT RSModifiersDrawThread final {
111 public:
112     static RSModifiersDrawThread& Instance();
113     static void SetCacheDir(const std::string& path);
114     static std::string GetCacheDir();
115 #ifdef ACCESSIBILITY_ENABLE
116     bool GetHighContrast() const;
117 #endif
118 
119     void PostTask(const std::function<void()>&& task, const std::string& name = std::string(), int64_t delayTime = 0);
120     void PostSyncTask(const std::function<void()>&& task);
121     void RemoveTask(const std::string& name);
122     bool GetIsStarted() const;
123     static bool GetIsFirstFrame();
124     static void SetIsFirstFrame(bool isFirstFrame);
125     template<typename Task, typename Return = std::invoke_result_t<Task>>
ScheduleTask(Task && task)126     std::future<Return> ScheduleTask(Task&& task)
127     {
128         auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task));
129         PostTask([scheduledTask_(std::move(scheduledTask))]() { scheduledTask_->Run(); });
130         return std::move(taskFuture);
131     }
132 
133     static std::unique_ptr<RSTransactionData>& ConvertTransaction(std::unique_ptr<RSTransactionData>& transactionData,
134         std::shared_ptr<RSIRenderClient> renderServiceClient, bool& isNeedCommit);
135 
136     static std::recursive_mutex transactionDataMutex_;
137 private:
138     RSModifiersDrawThread();
139     ~RSModifiersDrawThread();
140     static void Destroy();
141     RSModifiersDrawThread(const RSModifiersDrawThread&) = delete;
142     RSModifiersDrawThread(const RSModifiersDrawThread&&) = delete;
143     RSModifiersDrawThread& operator=(const RSModifiersDrawThread&) = delete;
144     RSModifiersDrawThread& operator=(const RSModifiersDrawThread&&) = delete;
145 
146     void ClearEventResource();
147 #ifdef ACCESSIBILITY_ENABLE
148     void SubscribeHighContrastChange();
149     void UnsubscribeHighContrastChange();
150 #endif
151 
152     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
153     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
154     std::mutex mutex_;
155     static std::atomic<bool> isStarted_;
156     static bool isFirstFrame_;
157     static std::string cacheDir_;
158     static std::shared_mutex cacheDirMtx_;
159 
160 #ifdef ACCESSIBILITY_ENABLE
161     bool highContrast_ = false;
162     std::shared_ptr<Detail::HighContrastObserver> highContrastObserver_ = nullptr;
163 #endif
164 
165     void Start();
166 };
167 } // namespace Rosen
168 } // namespace OHOS
169 #endif // RS_MODIFIERS_DRAW_THREAD_H
170