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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_WINDOW_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_WINDOW_H 18 19 #include <memory> 20 21 #include "base/geometry/ng/rect_t.h" 22 #include "base/mousestyle/mouse_style.h" 23 #include "base/thread/task_executor.h" 24 #include "base/utils/macros.h" 25 #include "base/utils/noncopyable.h" 26 #include "core/common/ace_page.h" 27 #include "core/common/platform_window.h" 28 29 namespace OHOS::Rosen { 30 class RSUIDirector; 31 } 32 33 namespace OHOS::Ace { 34 35 namespace NG { 36 class FrameNode; 37 } // namespace NG 38 39 enum class WidthBreakpoint {WIDTH_XS, WIDTH_SM, WIDTH_MD, WIDTH_LG, WIDTH_XL, WIDTH_XXL, UNDEFINED}; 40 enum class HeightBreakpoint {HEIGHT_SM, HEIGHT_MD, HEIGHT_LG}; 41 42 class ACE_EXPORT Window : public std::enable_shared_from_this<Window> { 43 public: 44 Window() = default; 45 explicit Window(std::unique_ptr<PlatformWindow> platformWindow); 46 virtual ~Window() = default; 47 GetWindowId()48 uint32_t GetWindowId() const 49 { 50 return windowId_; 51 } 52 53 virtual void RequestFrame(); ForceFlushVsync(uint64_t nanoTimestamp,uint64_t frameCount)54 virtual void ForceFlushVsync(uint64_t nanoTimestamp, uint64_t frameCount) {} 55 FlushFrameRate(int32_t rate,int32_t animatorExpectedFrameRate,int32_t rateTyte)56 virtual void FlushFrameRate(int32_t rate, int32_t animatorExpectedFrameRate, int32_t rateTyte) {} 57 SetTaskExecutor(const RefPtr<TaskExecutor> & taskExecutor)58 virtual void SetTaskExecutor(const RefPtr<TaskExecutor>& taskExecutor) {} 59 SetInstanceId(int32_t instanceId)60 virtual void SetInstanceId(int32_t instanceId) {} 61 Init()62 virtual void Init() {} 63 InitArkUI_X()64 virtual void InitArkUI_X() {} 65 Destroy()66 virtual void Destroy() 67 { 68 platformWindow_->Destroy(); 69 platformWindow_.reset(); 70 } 71 72 virtual void SetRootRenderNode(const RefPtr<RenderNode>& root); 73 SetRootFrameNode(const RefPtr<NG::FrameNode> & root)74 virtual void SetRootFrameNode(const RefPtr<NG::FrameNode>& root) {} 75 RecordFrameTime(uint64_t timeStamp,const std::string & name)76 virtual void RecordFrameTime(uint64_t timeStamp, const std::string& name) {} 77 78 virtual void FlushTasks(std::function<void()> callback = nullptr) {} 79 GetRSUIDirector()80 virtual std::shared_ptr<Rosen::RSUIDirector> GetRSUIDirector() const 81 { 82 return nullptr; 83 } 84 FlushLayoutSize(int32_t width,int32_t height)85 virtual void FlushLayoutSize(int32_t width, int32_t height) {} 86 FlushAnimation(uint64_t timeStamp)87 virtual bool FlushAnimation(uint64_t timeStamp) 88 { 89 return false; 90 } 91 HasFirstFrameAnimation()92 virtual bool HasFirstFrameAnimation() 93 { 94 return false; 95 } 96 FlushAnimationStartTime(uint64_t timeStamp)97 virtual void FlushAnimationStartTime(uint64_t timeStamp) {} 98 FlushModifier()99 virtual void FlushModifier() {} 100 HasUIRunningAnimation()101 virtual bool HasUIRunningAnimation() 102 { 103 return false; 104 } 105 106 virtual void OnVsync(uint64_t nanoTimestamp, uint64_t frameCount); 107 108 virtual void SetVsyncCallback(AceVsyncCallback&& callback); 109 OnShow()110 virtual void OnShow() 111 { 112 onShow_ = true; 113 } 114 OnHide()115 virtual void OnHide() 116 { 117 onShow_ = false; 118 } 119 IsHide()120 bool IsHide() const 121 { 122 return !onShow_; 123 } 124 GetUiDvsyncSwitch()125 bool GetUiDvsyncSwitch() const 126 { 127 return dvsyncOn_; 128 } 129 130 int64_t GetDeadlineByFrameCount(int64_t deadline, int64_t ts, int64_t frameBufferCount); 131 SetDensity(double density)132 void SetDensity(double density) 133 { 134 density_ = density; 135 } 136 SetGetWindowRectImpl(std::function<Rect ()> && callback)137 void SetGetWindowRectImpl(std::function<Rect()>&& callback) 138 { 139 windowRectImpl_ = std::move(callback); 140 } 141 InitGetGlobalWindowRectCallback(std::function<Rect ()> && callback)142 void InitGetGlobalWindowRectCallback(std::function<Rect()>&& callback) 143 { 144 globalDisplayWindowRectImpl_ = std::move(callback); 145 } 146 GetCurrentWindowRect()147 virtual Rect GetCurrentWindowRect() const 148 { 149 Rect rect; 150 if (windowRectImpl_) { 151 rect = windowRectImpl_(); 152 } 153 return rect; 154 } 155 GetGlobalDisplayWindowRect()156 virtual Rect GetGlobalDisplayWindowRect() const 157 { 158 Rect rect; 159 if (globalDisplayWindowRectImpl_) { 160 rect = globalDisplayWindowRectImpl_(); 161 } 162 return rect; 163 } 164 SetDrawTextAsBitmap(bool useBitmap)165 virtual void SetDrawTextAsBitmap(bool useBitmap) {} 166 GetRefreshRate()167 virtual float GetRefreshRate() const 168 { 169 return 0.0f; 170 } 171 GetLastRequestVsyncTime()172 uint64_t GetLastRequestVsyncTime() const 173 { 174 return lastRequestVsyncTime_; 175 } 176 GetLastVsyncEndTimestamp()177 int64_t GetLastVsyncEndTimestamp() const 178 { 179 return lastVsyncEndTimestamp_; 180 } 181 SetLastVsyncEndTimestamp(int64_t lastVsyncEndTimestamp)182 void SetLastVsyncEndTimestamp(int64_t lastVsyncEndTimestamp) 183 { 184 lastVsyncEndTimestamp_ = lastVsyncEndTimestamp; 185 } 186 SetKeepScreenOn(bool keepScreenOn)187 virtual void SetKeepScreenOn(bool keepScreenOn) {}; SetViewKeepScreenOn(bool keepScreenOn)188 virtual void SetViewKeepScreenOn(bool keepScreenOn) {}; 189 GetVSyncPeriod()190 virtual int64_t GetVSyncPeriod() const 191 { 192 return 0; 193 }; 194 GetWindowName()195 virtual std::string GetWindowName() const 196 { 197 static std::string name; 198 return name; 199 }; 200 SetCursor(MouseFormat cursor)201 void SetCursor(MouseFormat cursor) 202 { 203 cursor_ = cursor; 204 } 205 GetCursor()206 MouseFormat GetCursor() const 207 { 208 return cursor_; 209 } 210 SetUserSetCursor(bool isUserSetCursor)211 void SetUserSetCursor(bool isUserSetCursor) 212 { 213 isUserSetCursor_ = isUserSetCursor; 214 } 215 IsUserSetCursor()216 bool IsUserSetCursor() const 217 { 218 return isUserSetCursor_; 219 } 220 GetCurrentRefreshRateMode()221 virtual int32_t GetCurrentRefreshRateMode() const 222 { 223 return -1; 224 } 225 GetAnimateExpectedRate()226 virtual int32_t GetAnimateExpectedRate() const 227 { 228 return 0; 229 } 230 Lock()231 virtual void Lock() {} 232 Unlock()233 virtual void Unlock() {} 234 235 virtual void SetUiDvsyncSwitch(bool dvsyncSwitch); 236 GetStatusBarHeight()237 virtual uint32_t GetStatusBarHeight() const 238 { 239 return 0; 240 } 241 GetIsRequestVsync()242 virtual bool GetIsRequestVsync() 243 { 244 return false; 245 } 246 NotifyExtensionTimeout(int32_t errorCode)247 virtual void NotifyExtensionTimeout(int32_t errorCode) {} 248 NotifySnapshotUpdate()249 virtual void NotifySnapshotUpdate() {} 250 GetIsRequestFrame()251 virtual bool GetIsRequestFrame() 252 { 253 return false; 254 } 255 256 void SetForceVsyncRequests(bool forceVsyncRequests); 257 258 WidthBreakpoint GetWidthBreakpoint(const WidthLayoutBreakPoint& layoutBreakpoints) const; 259 HeightBreakpoint GetHeightBreakpoint(const HeightLayoutBreakPoint& layoutBreakpoints) const; 260 SetDVSyncUpdate(uint64_t dvsyncTime)261 virtual void SetDVSyncUpdate(uint64_t dvsyncTime) {} 262 protected: 263 bool isRequestVsync_ = false; 264 bool onShow_ = true; 265 double density_ = 1.0; 266 MouseFormat cursor_ = MouseFormat::DEFAULT; 267 bool isUserSetCursor_ = false; 268 269 struct VsyncCallback { 270 AceVsyncCallback callback_ = nullptr; 271 int32_t containerId_ = -1; 272 }; 273 std::list<struct VsyncCallback> callbacks_; 274 275 uint64_t lastRequestVsyncTime_ = 0; 276 int64_t lastVsyncEndTimestamp_ = 0; 277 uint32_t windowId_ = 0; 278 bool dvsyncOn_ = false; 279 int64_t lastDVsyncInbihitPredictTs_ = 0; 280 bool forceVsync_ = false; 281 282 private: 283 std::function<Rect()> windowRectImpl_; 284 std::function<Rect()> globalDisplayWindowRectImpl_; 285 std::unique_ptr<PlatformWindow> platformWindow_; 286 287 ACE_DISALLOW_COPY_AND_MOVE(Window); 288 }; 289 290 } // namespace OHOS::Ace 291 292 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_WINDOW_H 293