• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 OHOS_ROSEN_WINDOW_IMPL_H
17 #define OHOS_ROSEN_WINDOW_IMPL_H
18 
19 #include <map>
20 
21 #include <ability_context.h>
22 #include <i_input_event_consumer.h>
23 #include <key_event.h>
24 #include <refbase.h>
25 #include <ui_content.h>
26 #include <ui/rs_surface_node.h>
27 
28 #include "input_transfer_station.h"
29 #include "vsync_station.h"
30 #include "window.h"
31 #include "window_property.h"
32 #include "wm_common_inner.h"
33 #include "wm_common.h"
34 
35 namespace OHOS {
36 namespace Rosen {
37 union ColorParam {
38 #if BIG_ENDIANNESS
39     struct {
40         uint8_t alpha;
41         uint8_t red;
42         uint8_t green;
43         uint8_t blue;
44     } argb;
45 #else
46     struct {
47         uint8_t blue;
48         uint8_t green;
49         uint8_t red;
50         uint8_t alpha;
51     } argb;
52 #endif
53     uint32_t value;
54 };
55 
56 class WindowImpl : public Window {
57 #define CALL_LIFECYCLE_LISTENER(windowLifecycleCb)              \
58     do {                                                        \
59         for (auto& listener : lifecycleListeners_) {            \
60             if (listener != nullptr) {                          \
61                 listener->windowLifecycleCb();                  \
62             }                                                   \
63         }                                                       \
64     } while (0)
65 
66 #define CALL_UI_CONTENT(uiContentCb)                            \
67     do {                                                        \
68         if (uiContent_ != nullptr) {                            \
69             uiContent_->uiContentCb();                          \
70         }                                                       \
71     } while (0)
72 
73 public:
74     WindowImpl(const sptr<WindowOption>& option);
75     ~WindowImpl();
76 
77     static sptr<Window> Find(const std::string& id);
78     static sptr<Window> GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context = nullptr);
79     static sptr<Window> GetTopWindowWithId(uint32_t mainWinId);
80     static std::vector<sptr<Window>> GetSubWindow(uint32_t parantId);
81     virtual std::shared_ptr<RSSurfaceNode> GetSurfaceNode() const override;
82     virtual Rect GetRect() const override;
83     virtual WindowType GetType() const override;
84     virtual WindowMode GetMode() const override;
85     virtual WindowBlurLevel GetWindowBackgroundBlur() const override;
86     virtual float GetAlpha() const override;
87     virtual bool GetShowState() const override;
88     virtual WMError SetFocusable(bool isFocusable) override;
89     virtual bool GetFocusable() const override;
90     virtual WMError SetTouchable(bool isTouchable) override;
91     virtual bool GetTouchable() const override;
92     virtual const std::string& GetWindowName() const override;
93     virtual uint32_t GetWindowId() const override;
94     virtual uint32_t GetWindowFlags() const override;
GetNativeDestroyCallback()95     inline NotifyNativeWinDestroyFunc GetNativeDestroyCallback()
96     {
97         return notifyNativefunc_;
98     }
99     virtual SystemBarProperty GetSystemBarPropertyByType(WindowType type) const override;
100     virtual bool IsFullScreen() const override;
101     virtual bool IsLayoutFullScreen() const override;
102     virtual WMError SetWindowType(WindowType type) override;
103     virtual WMError SetWindowMode(WindowMode mode) override;
104     virtual WMError SetWindowBackgroundBlur(WindowBlurLevel level) override;
105     virtual WMError SetAlpha(float alpha) override;
106     virtual WMError AddWindowFlag(WindowFlag flag) override;
107     virtual WMError RemoveWindowFlag(WindowFlag flag) override;
108     virtual WMError SetWindowFlags(uint32_t flags) override;
109     virtual WMError SetSystemBarProperty(WindowType type, const SystemBarProperty& property) override;
110     virtual WMError SetLayoutFullScreen(bool status) override;
111     virtual WMError SetFullScreen(bool status) override;
SetWindowState(WindowState state)112     inline void SetWindowState(WindowState state)
113     {
114         state_ = state;
115     }
116     virtual WMError GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea) override;
117 
118     WMError Create(const std::string& parentName,
119         const std::shared_ptr<AbilityRuntime::Context>& context = nullptr);
120     virtual WMError Destroy() override;
121     virtual WMError Show(uint32_t reason = 0) override;
122     virtual WMError Hide(uint32_t reason = 0) override;
123     virtual WMError MoveTo(int32_t x, int32_t y) override;
124     virtual WMError Resize(uint32_t width, uint32_t height) override;
125     virtual WMError SetKeepScreenOn(bool keepScreenOn) override;
126     virtual bool IsKeepScreenOn() const override;
127     virtual WMError SetTurnScreenOn(bool turnScreenOn) override;
128     virtual bool IsTurnScreenOn() const override;
129     virtual WMError SetBackgroundColor(const std::string& color) override;
130     virtual WMError SetTransparent(bool isTransparent) override;
131     virtual bool IsTransparent() const override;
132     virtual WMError SetBrightness(float brightness) override;
133     virtual float GetBrightness() const override;
134     virtual WMError SetCallingWindow(uint32_t windowId) override;
135     virtual void SetPrivacyMode(bool isPrivacyMode) override;
136     virtual bool IsPrivacyMode() const override;
137 
138     virtual bool IsDecorEnable() const override;
139     virtual WMError Maximize() override;
140     virtual WMError Minimize() override;
141     virtual WMError Recover() override;
142     virtual WMError Close() override;
143     virtual void StartMove() override;
144 
145     virtual WMError RequestFocus() const override;
146     virtual void AddInputEventListener(const std::shared_ptr<MMI::IInputEventConsumer>& inputEventListener) override;
147 
148     virtual void RegisterLifeCycleListener(sptr<IWindowLifeCycle>& listener) override;
149     virtual void RegisterWindowChangeListener(sptr<IWindowChangeListener>& listener) override;
150     virtual void UnregisterLifeCycleListener(sptr<IWindowLifeCycle>& listener) override;
151     virtual void UnregisterWindowChangeListener(sptr<IWindowChangeListener>& listener) override;
152     virtual void RegisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener) override;
153     virtual void UnregisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener) override;
154     virtual void RegisterDragListener(const sptr<IWindowDragListener>& listener) override;
155     virtual void UnregisterDragListener(const sptr<IWindowDragListener>& listener) override;
156     virtual void RegisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener) override;
157     virtual void UnregisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener) override;
158     virtual void RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc& func) override;
159     virtual void RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener) override;
160     virtual void UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener) override;
161     virtual void SetAceAbilityHandler(const sptr<IAceAbilityHandler>& handler) override;
162     void UpdateRect(const struct Rect& rect, WindowSizeChangeReason reason);
163     void UpdateMode(WindowMode mode);
164     virtual void ConsumeKeyEvent(std::shared_ptr<MMI::KeyEvent>& inputEvent) override;
165     virtual void ConsumePointerEvent(std::shared_ptr<MMI::PointerEvent>& inputEvent) override;
166     virtual void RequestFrame() override;
167     void UpdateFocusStatus(bool focused);
168     virtual void UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration) override;
169     void UpdateAvoidArea(const std::vector<Rect>& avoidAreas);
170     void UpdateWindowState(WindowState state);
171     void UpdateDragEvent(const PointInfo& point, DragEvent event);
172     void UpdateDisplayId(DisplayId from, DisplayId to);
173     void UpdateOccupiedAreaChangeInfo(const sptr<OccupiedAreaChangeInfo>& info);
174     void UpdateActiveStatus(bool isActive);
175 
176     virtual WMError SetUIContent(const std::string& contentInfo, NativeEngine* engine,
177         NativeValue* storage, bool isdistributed, AppExecFwk::Ability* ability) override;
178     virtual std::string GetContentInfo() override;
179     virtual const std::shared_ptr<AbilityRuntime::Context> GetContext() const override;
180     virtual Ace::UIContent* GetUIContent() const override;
181     virtual void SetRequestedOrientation(Orientation) override;
182     virtual Orientation GetRequestedOrientation() override;
183 
184     // colorspace, gamut
185     virtual bool IsSupportWideGamut() override;
186     virtual void SetColorSpace(ColorSpace colorSpace) override;
187     virtual ColorSpace GetColorSpace() override;
188 
189     virtual void DumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override;
190 private:
NotifyAfterForeground()191     inline void NotifyAfterForeground() const
192     {
193         CALL_LIFECYCLE_LISTENER(AfterForeground);
194         CALL_UI_CONTENT(Foreground);
195     }
NotifyAfterBackground()196     inline void NotifyAfterBackground() const
197     {
198         CALL_LIFECYCLE_LISTENER(AfterBackground);
199         CALL_UI_CONTENT(Background);
200     }
NotifyAfterFocused()201     inline void NotifyAfterFocused() const
202     {
203         CALL_LIFECYCLE_LISTENER(AfterFocused);
204         CALL_UI_CONTENT(Focus);
205     }
NotifyAfterUnfocused()206     inline void NotifyAfterUnfocused() const
207     {
208         CALL_LIFECYCLE_LISTENER(AfterUnfocused);
209         CALL_UI_CONTENT(UnFocus);
210     }
NotifyListenerAfterUnfocused()211     inline void NotifyListenerAfterUnfocused() const
212     {
213         CALL_LIFECYCLE_LISTENER(AfterUnfocused);
214     }
NotifyBeforeDestroy(std::string windowName)215     inline void NotifyBeforeDestroy(std::string windowName) const
216     {
217         CALL_UI_CONTENT(Destroy);
218         if (notifyNativefunc_) {
219             notifyNativefunc_(windowName);
220         }
221     }
NotifyBeforeSubWindowDestroy(sptr<WindowImpl> window)222     inline void NotifyBeforeSubWindowDestroy(sptr<WindowImpl> window) const
223     {
224         auto uiContent = window->GetUIContent();
225         if (uiContent != nullptr) {
226             uiContent->Destroy();
227         }
228         if (window->GetNativeDestroyCallback()) {
229             window->GetNativeDestroyCallback()(window->GetWindowName());
230         }
231     }
NotifyAfterActive()232     inline void NotifyAfterActive() const
233     {
234         CALL_LIFECYCLE_LISTENER(AfterActive);
235     }
NotifyAfterInactive()236     inline void NotifyAfterInactive() const
237     {
238         CALL_LIFECYCLE_LISTENER(AfterInactive);
239     }
240     void DestroyFloatingWindow();
241     void DestroySubWindow();
242     void SetDefaultOption(); // for api7
243     bool IsWindowValid() const;
244     void OnVsync(int64_t timeStamp);
245     static sptr<Window> FindTopWindow(uint32_t topWinId);
246     WMError Drag(const Rect& rect);
247     void ConsumeMoveOrDragEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent);
248     void HandleDragEvent(int32_t posX, int32_t posY, int32_t pointId);
249     void HandleMoveEvent(int32_t posX, int32_t posY, int32_t pointId);
250     void ReadyToMoveOrDragWindow(int32_t globalX, int32_t globalY, int32_t pointId, const Rect& rect);
251     void EndMoveOrDragWindow(int32_t pointId);
252     bool IsPointerEventConsumed();
253     void AdjustWindowAnimationFlag();
254     void MapFloatingWindowToAppIfNeeded();
255     WMError UpdateProperty(PropertyChangeAction action);
256     WMError Destroy(bool needNotifyServer);
257     WMError SetBackgroundColor(uint32_t color);
258     uint32_t GetBackgroundColor() const;
259 
260     // colorspace, gamut
261     using ColorSpaceConvertMap = struct {
262         ColorSpace colorSpace;
263         SurfaceColorGamut sufaceColorGamut;
264     };
265     static const ColorSpaceConvertMap colorSpaceConvertMap[];
266     static ColorSpace GetColorSpaceFromSurfaceGamut(SurfaceColorGamut surfaceColorGamut);
267     static SurfaceColorGamut GetSurfaceGamutFromColorSpace(ColorSpace colorSpace);
268 
269     std::shared_ptr<VsyncStation::VsyncCallback> callback_ =
270         std::make_shared<VsyncStation::VsyncCallback>(VsyncStation::VsyncCallback());
271     static std::map<std::string, std::pair<uint32_t, sptr<Window>>> windowMap_;
272     static std::map<uint32_t, std::vector<sptr<WindowImpl>>> subWindowMap_;
273     static std::map<uint32_t, std::vector<sptr<WindowImpl>>> appFloatingWindowMap_;
274     sptr<WindowProperty> property_;
275     WindowState state_ { WindowState::STATE_INITIAL };
276     WindowTag windowTag_;
277     sptr<IAceAbilityHandler> aceAbilityHandler_;
278     std::vector<sptr<IWindowLifeCycle>> lifecycleListeners_;
279     std::vector<sptr<IWindowChangeListener>> windowChangeListeners_;
280     std::vector<sptr<IAvoidAreaChangedListener>> avoidAreaChangeListeners_;
281     std::vector<sptr<IWindowDragListener>> windowDragListeners_;
282     std::vector<sptr<IDisplayMoveListener>> displayMoveListeners_;
283     std::vector<sptr<IOccupiedAreaChangeListener>> occupiedAreaChangeListeners_;
284     NotifyNativeWinDestroyFunc notifyNativefunc_;
285     std::shared_ptr<RSSurfaceNode> surfaceNode_;
286     std::string name_;
287     std::unique_ptr<Ace::UIContent> uiContent_;
288     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_; // give up when context offer getToken
289     std::shared_ptr<AbilityRuntime::Context> context_;
290     std::recursive_mutex mutex_;
291     const float SYSTEM_ALARM_WINDOW_WIDTH_RATIO = 0.8;
292     const float SYSTEM_ALARM_WINDOW_HEIGHT_RATIO = 0.3;
293 
294     int32_t startPointPosX_ = 0;
295     int32_t startPointPosY_ = 0;
296     int32_t startPointerId_ = 0;
297     bool startDragFlag_ = false;
298     bool startMoveFlag_ = false;
299     bool pointEventStarted_ = false;
300     Rect startPointRect_ = { 0, 0, 0, 0 };
301     Rect startRectExceptFrame_ = { 0, 0, 0, 0 };
302     Rect startRectExceptCorner_ = { 0, 0, 0, 0 };
303 };
304 }
305 }
306 #endif // OHOS_ROSEN_WINDOW_IMPL_H
307