• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 /**
17  * @addtogroup RenderNodeDisplay
18  * @{
19  *
20  * @brief Display render nodes.
21  */
22 
23 /**
24  * @file rs_ui_director.h
25  *
26  * @brief Defines the properties and methods for RSUIDirector class.
27  */
28 
29 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_DIRECTOR_H
30 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_DIRECTOR_H
31 
32 #include <atomic>
33 #include <functional>
34 #include <mutex>
35 
36 #include "command/rs_animation_command.h"
37 #include "common/rs_common_def.h"
38 #include "transaction/rs_irender_client.h"
39 
40 namespace OHOS {
41 class Surface;
42 namespace Rosen {
43 class RSSurfaceNode;
44 class RSRootNode;
45 class RSTransactionData;
46 class RSUIContext;
47 class RSTransactionHandler;
48 using TaskRunner = std::function<void(const std::function<void()>&, uint32_t)>;
49 using FlushEmptyCallback = std::function<bool(const uint64_t)>;
50 using CommitTransactionCallback =
51     std::function<void(std::shared_ptr<RSIRenderClient>&, std::unique_ptr<RSTransactionData>&&, uint32_t&,
52     std::shared_ptr<RSTransactionHandler>)>;
53 
54 /**
55  * @class RSUIDirector
56  *
57  * @brief The class for managing the UI director in the rendering service.
58  */
59 class RSC_EXPORT RSUIDirector final {
60 public:
61     /**
62      * @brief Creates and returns a shared pointer to an instance of RSUIDirector.
63      *
64      * @return A std::shared_ptr pointing to the newly created RSUIDirector instance.
65      */
66     static std::shared_ptr<RSUIDirector> Create();
67 
68     /**
69      * @brief Destructor for RSUIDirector.
70      */
71     ~RSUIDirector();
72 
73     /**
74      * @brief Moves the UI director to the background.
75      *
76      * Update the window status, node status, and clear cache and redundant resources
77      *
78      * @param isTextureExport Indicates whether texture export should be performed
79      *                        during the transition to the background. Defaults to false.
80      */
81     void GoBackground(bool isTextureExport = false);
82 
83     /**
84      * @brief Moves the UI director to the foreground.
85      *
86      * @param isTextureExport Indicates whether texture export should be performed
87      *                        during the transition to the foreground. Defaults to false.
88      */
89     void GoForeground(bool isTextureExport = false);
90 
91     /**
92      * @brief Initializes the RSUIDirector instance.
93      *
94      * @param shouldCreateRenderThread Indicates whether a render thread should be created. Defaults to true.
95      * @param isMultiInstance Indicates whether the instance supports multiple instances. Defaults to false.
96      */
97     void Init(bool shouldCreateRenderThread = true, bool isMultiInstance = false);
98 
99     /**
100      * @brief Initiates the process of exporting texture data.
101      * @param rsUIContext A shared pointer to the rsUIContext object to be set.
102      */
103     void StartTextureExport(std::shared_ptr<RSUIContext> rsUIContext = nullptr);
104 
105     /**
106      * @brief Destroy RSUIDirector instance.
107      */
108     void Destroy(bool isTextureExport = false);
109 
110     /**
111      * @brief Sets the surfaceNode for the RSUIDirector.
112      *
113      * @param surfaceNode A shared pointer to the Surface object to be set.
114      */
115     void SetRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode);
116     std::shared_ptr<RSSurfaceNode> GetRSSurfaceNode() const;
117 
118     /**
119      * @brief Sets the background alpha value for the ability.
120      *
121      * @param alpha The alpha value to set.
122      */
123     void SetAbilityBGAlpha(uint8_t alpha);
124 
125     /**
126      * @brief Set rt render status and keep it till set again
127      *
128      * @param isRenderForced if true, rt will reject partial render and be forced to render all frames
129      */
130     void SetRTRenderForced(bool isRenderForced);
131 
132     /**
133      * @brief Sets the container window properties.
134      *
135      * @param hasContainerWindow A boolean indicating whether the container window is present.
136      * @param rrect The rounded rectangle (RRect) defining the bounds of the container window.
137      */
138     void SetContainerWindow(bool hasContainerWindow, RRect rrect);
139 
140     /**
141      * @brief Sets the callback function to be invoked when a flush operation results in no changes.
142      *
143      * @param flushEmptyCallback The callback function to be executed on an empty flush.
144      */
145     void SetFlushEmptyCallback(FlushEmptyCallback flushEmptyCallback);
146 
147     /**
148      * @brief Sets the root node for the UI director.
149      *
150      * It is recommended to use the SetRSRootNode interface, as the SetRoot interface is planned to be deprecated.
151      *
152      * @param root The ID of the node to be set as the root.
153      */
154     void SetRoot(NodeId root);
155 
156     /**
157      * @brief Sets the UI task runner for the specified instance.
158      *
159      * plan to del.
160      *
161      * @param uiTaskRunner The task runner to be used for UI tasks.
162      * @param instanceId The ID of the instance for which the task runner is being set.
163      *                   Defaults to INSTANCE_ID_UNDEFINED if not specified.
164      * @param useMultiInstance A flag indicating whether to use multiple instances. Defaults to false.
165      */
166     void SetUITaskRunner(const TaskRunner& uiTaskRunner, int32_t instanceId = INSTANCE_ID_UNDEFINED,
167         bool useMultiInstance = false);
168 
169     /**
170      * @brief Post messages to render thread.
171      */
172     void SendMessages();
173 
174     /**
175      * @brief Post messages to render thread with callback.
176      *
177      * @param callback The callback value to be set.
178      */
179     void SendMessages(std::function<void()> callback);
180 
181     /**
182      * @brief Sets the timestamp and associates it with a specific ability name.
183      *
184      * @param timeStamp The timestamp value to be set.
185      * @param abilityName The name of the ability to associate with the timestamp.
186      */
187     void SetTimeStamp(uint64_t timeStamp, const std::string& abilityName);
188 
189     /**
190      * @brief Sets the directory path for caching files.
191      *
192      * @param cacheFilePath The file path to the cache directory.
193      */
194     void SetCacheDir(const std::string& cacheFilePath);
195 
196     /**
197      * @brief Flushes the animation updates to the rendering pipeline.
198      *
199      * @param timeStamp The current timestamp.
200      * @param vsyncPeriod The vertical synchronization period, in nanoseconds. defaults to 0 if not provided.
201      * @return true if the animation flush was successful, false otherwise.
202      */
203     bool FlushAnimation(uint64_t timeStamp, int64_t vsyncPeriod = 0);
204 
205     /**
206      * @brief Checks if the first frame animation is present.
207      *
208      * @return true if the first frame animation exists, false otherwise.
209      */
210     bool HasFirstFrameAnimation();
211 
212     /**
213      * @brief Flushes the modifier operations to ensure that all pending changes are applied.
214      *
215      * Sends the modifier commands to the render service for drawing.
216      */
217     void FlushModifier();
218 
219     /**
220      * @brief Checks if there are any UI animations currently running.
221      *
222      * @return true if there are running UI animations, false otherwise.
223      */
224     bool HasUIRunningAnimation();
225 
226     /**
227      * @brief Updates the start time of animations with the specified timestamp.
228      *
229      * @param timeStamp The timestamp (in microseconds) to set as the start time for animations.
230      */
231     void FlushAnimationStartTime(uint64_t timeStamp);
232 
233     /**
234      * @brief Sets the application freeze state.
235      *
236      * @param isAppFreeze Indicates the freeze state of the application.
237      *                    True if the application is frozen; false otherwise.
238      */
239     void SetAppFreeze(bool isAppFreeze);
240 
241     /**
242      * @brief Sets the callback function to be invoked when a vsync request is made.
243      *
244      * @param callback A std::function object representing the callback to be executed.
245      */
246     void SetRequestVsyncCallback(const std::function<void()>& callback);
247 
248     /**
249      * @brief Posts a task to handle frame rate operations.
250      *
251      * @param task The task to be executed, represented as a std::function<void()>.
252      * @param useMultiInstance Optional parameter indicating whether to use multiple
253      *                         instances for the task execution. Defaults to false.
254      */
255     static void PostFrameRateTask(const std::function<void()>& task, bool useMultiInstance = false);
256 
257     /**
258      * @brief Gets the current refresh rate mode of the UI director.
259      *
260      * @return int32_t The current refresh rate mode.
261      */
262     int32_t GetCurrentRefreshRateMode();
263 
264     /**
265      * @brief Gets the expected animation frame rate.
266      *
267      * @return The expected frame rate for animations as an integer.
268      */
269     int32_t GetAnimateExpectedRate() const;
270 
271     /**
272      * @brief Gets the index associated with the current object.
273      *
274      * @return The index as an unsigned 32-bit integer.
275      */
276     uint32_t GetIndex() const;
277 
278     /**
279      * @brief Gets the RSUIContext associated with this RSUIDirector.
280      *
281      * @return A shared pointer to the RSUIContext instance.
282      */
283     std::shared_ptr<RSUIContext> GetRSUIContext() const;
284 
285     /**
286      * @brief Sets the root node for the UI director.
287      *
288      * @param rootNode A std::shared_ptr pointing to the RSRootNode object to be set.
289      */
290     void SetRSRootNode(std::shared_ptr<RSRootNode> rootNode);
291 
292     /**
293      * @brief Identify typical resident processes of the system, such as FSR, SCB, inputMethod.
294      *
295      * @param isTypicalResidentProcess means whether the relevant services are disabled.
296      */
297     static void SetTypicalResidentProcess(bool isTypicalResidentProcess = false);
298 
299     /**
300      * @brief Checks whether hybrid render is enabled.
301      *
302      * @return true if hybrid render is enabled, false otherwise.
303      */
304     static bool IsHybridRenderEnabled();
305 
306     /**
307      * @brief Checks whether hybrid render is enabled on componentEnableSwitch item.
308      *
309      * @param bitSeq ComponentEnableSwitch value .
310      * @return true if hybrid render is enabled, false otherwise.
311      */
312     static bool GetHybridRenderSwitch(ComponentEnableSwitch bitSeq);
313 
314     /**
315      * @brief Gets the textBlob length count that is supported when hybrid render is enabled.
316      *
317      * @return the textBlob length count that is supported.
318      */
319     static uint32_t GetHybridRenderTextBlobLenCount();
320 
321     /**
322      * @brief Sets the dvsynctime to command which will update the dvsync time.
323      *
324      * @param dvsyncTime the time which need send to dvsync.
325      */
326     void SetDVSyncUpdate(uint64_t dvsyncTime);
327 private:
328     void ReportUiSkipEvent(const std::string& abilityName);
329     void AttachSurface();
330     static void RecvMessages();
331     static void RecvMessages(std::shared_ptr<RSTransactionData> cmds);
332     static void ProcessInstanceMessages(
333         std::map<int32_t, std::vector<std::unique_ptr<RSCommand>>>& cmdMap, uint32_t messageId);
334     static void ProcessUIContextMessages(
335         std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>>& cmdMap, uint32_t messageId);
336     static void ProcessMessages(std::shared_ptr<RSTransactionData> cmds); // receive message
337     static void AnimationCallbackProcessor(NodeId nodeId, AnimationId animId, uint64_t token,
338         AnimationCallbackEvent event);
339     static void DumpNodeTreeProcessor(NodeId nodeId, pid_t pid, uint64_t token, uint32_t taskId);        // DFX to do
340     static void PostTask(const std::function<void()>& task, int32_t instanceId = INSTANCE_ID_UNDEFINED); // planing
341     static void PostDelayTask(
342         const std::function<void()>& task, uint32_t delay = 0, int32_t instanceId = INSTANCE_ID_UNDEFINED); // planing
343     static void SetTypicalResidentProcessOnce(bool isResidentProcess);
344 
345     void InitHybridRender();
346     void SetCommitTransactionCallback(CommitTransactionCallback commitTransactionCallback);
347 
348     RSUIDirector() = default;
349     RSUIDirector(const RSUIDirector&) = delete;
350     RSUIDirector(const RSUIDirector&&) = delete;
351     RSUIDirector& operator=(const RSUIDirector&) = delete;
352     RSUIDirector& operator=(const RSUIDirector&&) = delete;
353 
354     inline static std::unordered_map<RSUIDirector*, TaskRunner> uiTaskRunners_;
355     inline static std::mutex uiTaskRunnersVisitorMutex_;
356 
357     std::mutex mutex_;
358     NodeId root_ = 0;
359     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
360 
361     bool isActive_ = false;
362     bool isUniRenderEnabled_ = false;
363     uint64_t refreshPeriod_ = 16666667;
364     uint64_t timeStamp_ = 0;
365     int64_t lastUiSkipTimestamp_ = 0; // ms
366     uint32_t index_ = 0;
367     std::string abilityName_;
368     std::weak_ptr<RSSurfaceNode> surfaceNode_;
369     int surfaceWidth_ = 0;
370     int surfaceHeight_ = 0;
371     std::string cacheDir_;
372     static std::function<void()> requestVsyncCallback_;
373     bool isHgmConfigChangeCallbackReg_ = false;
374     std::shared_ptr<RSUIContext> rsUIContext_ = nullptr;
375     std::weak_ptr<RSRootNode> rootNode_;
376     bool dvsyncUpdate_ = false;
377     uint64_t dvsyncTime_ = 0;
378 
379     friend class RSApplicationAgentImpl;
380     friend class RSRenderThread;
381     friend class RSImplicitAnimator;
382 };
383 } // namespace Rosen
384 } // namespace OHOS
385 
386 /** @} */
387 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_DIRECTOR_H
388