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