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 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_DIRECTOR_H
16 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_DIRECTOR_H
17 
18 #include <atomic>
19 #include <functional>
20 #include <mutex>
21 
22 #include "command/rs_animation_command.h"
23 #include "common/rs_common_def.h"
24 
25 namespace OHOS {
26 class Surface;
27 namespace Rosen {
28 class RSSurfaceNode;
29 class RSTransactionData;
30 using TaskRunner = std::function<void(const std::function<void()>&, uint32_t)>;
31 using FlushEmptyCallback = std::function<bool(const uint64_t)>;
32 
33 class RSC_EXPORT RSUIDirector final {
34 public:
35     static std::shared_ptr<RSUIDirector> Create();
36 
37     ~RSUIDirector();
38     void GoBackground(bool isTextureExport = false);
39     void GoForeground(bool isTextureExport = false);
40     void Init(bool shouldCreateRenderThread = true);
41     void StartTextureExport();
42     void Destroy(bool isTextureExport = false);
43     void SetRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode);
44     void SetAbilityBGAlpha(uint8_t alpha);
45     /**
46      * @brief Set rt render status and keep it till set again
47      *
48      * @param isRenderForced if true, rt will reject partial render and be forced to render all frames
49      */
50     void SetRTRenderForced(bool isRenderForced);
51     void SetContainerWindow(bool hasContainerWindow, float density);
52     void SetFlushEmptyCallback(FlushEmptyCallback flushEmptyCallback);
53 
54     void SetRoot(NodeId root);
55     void SetUITaskRunner(const TaskRunner& uiTaskRunner, int32_t instanceId = INSTANCE_ID_UNDEFINED);
56     void SendMessages(); // post messages to render thread
57 
58     void SetTimeStamp(uint64_t timeStamp, const std::string& abilityName);
59     void SetCacheDir(const std::string& cacheFilePath);
60 
61     bool FlushAnimation(uint64_t timeStamp, int64_t vsyncPeriod = 0);
62     void FlushModifier();
63     bool HasUIRunningAnimation();
64     void FlushAnimationStartTime(uint64_t timeStamp);
65 
66     void SetAppFreeze(bool isAppFreeze);
67 
68     void SetRequestVsyncCallback(const std::function<void()>& callback);
69 
70     static void PostFrameRateTask(const std::function<void()>& task);
71 
72     int32_t GetCurrentRefreshRateMode();
73     int32_t GetAnimateExpectedRate() const;
74 
75     uint32_t GetIndex() const;
76 
77 private:
78     void AttachSurface();
79     static void RecvMessages();
80     static void RecvMessages(std::shared_ptr<RSTransactionData> cmds);
81     static void ProcessMessages(std::shared_ptr<RSTransactionData> cmds); // receive message
82     static void AnimationCallbackProcessor(NodeId nodeId, AnimationId animId, AnimationCallbackEvent event);
83     static void DumpNodeTreeProcessor(NodeId nodeId, pid_t pid, uint32_t taskId);
84     static void PostTask(const std::function<void()>& task, int32_t instanceId = INSTANCE_ID_UNDEFINED);
85     static void PostDelayTask(
86         const std::function<void()>& task, uint32_t delay = 0, int32_t instanceId = INSTANCE_ID_UNDEFINED);
87 
88     RSUIDirector() = default;
89     RSUIDirector(const RSUIDirector&) = delete;
90     RSUIDirector(const RSUIDirector&&) = delete;
91     RSUIDirector& operator=(const RSUIDirector&) = delete;
92     RSUIDirector& operator=(const RSUIDirector&&) = delete;
93 
94     std::mutex mutex_;
95     NodeId root_ = 0;
96     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
97 
98     bool isActive_ = false;
99     bool isUniRenderEnabled_ = false;
100     uint64_t refreshPeriod_ = 16666667;
101     uint64_t timeStamp_ = 0;
102     uint32_t index_ = 0;
103     std::string abilityName_;
104     std::weak_ptr<RSSurfaceNode> surfaceNode_;
105     int surfaceWidth_ = 0;
106     int surfaceHeight_ = 0;
107     std::string cacheDir_;
108     static std::function<void()> requestVsyncCallback_;
109     bool isHgmConfigChangeCallbackReg_ = false;
110 
111     friend class RSApplicationAgentImpl;
112     friend class RSRenderThread;
113     friend class RSImplicitAnimator;
114 };
115 } // namespace Rosen
116 } // namespace OHOS
117 
118 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_UI_DIRECTOR_H
119