• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file root_view.h
28  *
29  * @brief Manages a root view.
30  *
31  * A root view is the view containing its child views. It represents the root node in a tree structure and is
32  * the parent to all the children.
33  *
34  * @since 1.0
35  * @version 1.0
36  */
37 
38 #ifndef GRAPHIC_LITE_ROOT_VIEW_H
39 #define GRAPHIC_LITE_ROOT_VIEW_H
40 
41 #if defined __linux__ || defined __LITEOS__ || defined __APPLE__
42 #include <pthread.h>
43 #endif
44 
45 #include "components/ui_view_group.h"
46 #include "events/key_event.h"
47 #include "events/virtual_device_event.h"
48 #include "gfx_utils/list.h"
49 
50 #if LOCAL_RENDER
51 #include <map>
52 #include "gfx_utils/vector.h"
53 #endif
54 
55 namespace OHOS {
56 #if ENABLE_WINDOW
57 class Window;
58 class WindowImpl;
59 #endif
60 
61 /**
62  * @brief Defines the functions related to a root view which contains its child views and represents
63  *        the root node in a tree structure.
64  *
65  * @since 1.0
66  * @version 1.0
67  */
68 class RootView : public UIViewGroup {
69 public:
70     /**
71      * @brief Obtains a singleton <b>RootView</b> instance.
72      *
73      * @return Returns the singleton <b>RootView</b> instance.
74      * @since 1.0
75      * @version 1.0
76      */
77     static RootView* GetInstance();
78 
79 #if ENABLE_WINDOW
80     /**
81      * @brief Obtains a <b>RootView</b> instance bound to a window.
82      *
83      * @return Returns the <b>RootView</b> instance.
84      * @since 1.0
85      * @version 1.0
86      */
GetWindowRootView()87     static RootView* GetWindowRootView()
88     {
89         return new RootView;
90     }
91 
92     /**
93      * @brief Destroys the <b>RootView</b> bound to a window.
94      *
95      * @param rootView Indicates the pointer to the <b>RootView</b> to destroy.
96      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
97      * @since 1.0
98      * @version 1.0
99      */
DestoryWindowRootView(RootView * rootView)100     static bool DestoryWindowRootView(RootView* rootView)
101     {
102         if (rootView == RootView::GetInstance()) {
103             return false;
104         }
105         delete rootView;
106         return true;
107     }
108 #endif
109 
110     /**
111      * @brief Represents the listener for monitoring physical key events.
112      *
113      * @since 1.0
114      * @version 1.0
115      */
116     class OnKeyActListener : public HeapBase {
117     public:
118         /**
119          * @brief Responds to a physical key event.
120          *
121          * @param view Indicates the view displayed upon a physical key event.
122          * @param event Indicates the physical key event to respond to.
123          * @return Returns <b>true</b> if the view is normally displayed upon a physical key
124          *         event; returns <b> false</b> otherwise.
125          * @since 1.0
126          * @version 1.0
127          */
128         virtual bool OnKeyAct(UIView& view, const KeyEvent& event) = 0;
129     };
130 
131     /**
132      * @brief Obtains the view type.
133      *
134      * @return Returns <b>UI_ROOT_VIEW</b>, as defined in {@link UIViewType}.
135      * @since 1.0
136      * @version 1.0
137      */
GetViewType()138     UIViewType GetViewType() const override
139     {
140         return UI_ROOT_VIEW;
141     }
142 
143     /**
144      * @brief Executes a physical key event.
145      *
146      * @param event Indicates the physical key event to execute.
147      * @since 1.0
148      * @version 1.0
149      */
OnKeyEvent(const KeyEvent & event)150     virtual void OnKeyEvent(const KeyEvent& event)
151     {
152         if (onKeyActListener_ != nullptr) {
153             onKeyActListener_->OnKeyAct(*this, event);
154         }
155     }
156 
157     /**
158      * @brief Sets the listener that contains a callback to be invoked upon a physical key event.
159      *
160      * @param onKeyActListener Indicates the pointer to the listener to set.
161      * @since 1.0
162      * @version 1.0
163      */
SetOnKeyActListener(OnKeyActListener * onKeyActListener)164     void SetOnKeyActListener(OnKeyActListener* onKeyActListener)
165     {
166         onKeyActListener_ = onKeyActListener;
167     }
168 
169     /**
170      * @brief Clears the listener for monitoring physical key events.
171      *
172      * @since 1.0
173      * @version 1.0
174      */
ClearOnKeyActListener()175     void ClearOnKeyActListener()
176     {
177         onKeyActListener_ = nullptr;
178     }
179 
180     /**
181      * @brief Listens for the input events triggered by a virtual device other than
182      *        human touching or physical pressing.
183      *
184      * @since 1.0
185      * @version 1.0
186      */
187     class OnVirtualDeviceEventListener : public HeapBase {
188     public:
189         /**
190          * @brief Responds to an input event triggered by a virtual device.
191          *
192           * @param view Indicates the view displayed upon an input event triggered by a virtual device.
193          * @param event Indicates the input event to respond to.
194          * @return Returns <b>true</b> if this view is normally displayed upon an input event triggered by a
195          *         virtual device; returns <b> false</b> otherwise.
196          * @since 1.0
197          * @version 1.0
198          */
199         virtual bool OnVirtualDeviceEvent(UIView& view, VirtualDeviceEvent event) = 0;
200     };
201 
202     /**
203      * @brief Executes an input event triggered by a virtual device.
204      *
205      * @param event Indicates the input event to respond to.
206      * @since 1.0
207      * @version 1.0
208      */
OnVirtualDeviceEvent(const VirtualDeviceEvent & event)209     virtual void OnVirtualDeviceEvent(const VirtualDeviceEvent& event)
210     {
211         if (onVirtualEventListener_ != nullptr) {
212             onVirtualEventListener_->OnVirtualDeviceEvent(*this, event);
213         }
214     }
215 
216     /**
217      * @brief Sets a listener for monitoring the input events of a virtual device.
218      *
219      * @param onVirtualDeviceEventListener Indicates the pointer to the listener to set.
220      * @since 1.0
221      * @version 1.0
222      */
SetOnVirtualDeviceEventListener(OnVirtualDeviceEventListener * onVirtualDeviceEventListener)223     void SetOnVirtualDeviceEventListener(OnVirtualDeviceEventListener* onVirtualDeviceEventListener)
224     {
225         onVirtualEventListener_ = onVirtualDeviceEventListener;
226     }
227 
228     /**
229      * @brief Clears the listener for monitoring the input events of a virtual device.
230      *
231      * @since 1.0
232      * @version 1.0
233      */
ClearOnVirtualDeviceEventListener()234     void ClearOnVirtualDeviceEventListener()
235     {
236         onVirtualEventListener_ = nullptr;
237     }
238 
239     /**
240      * @brief Checks whether the target view is one of the child views of the specified parent view.
241      *
242      * @param parentView Indicates the specified parent view.
243      * @param subView Indicates the target child view.
244      * @return Returns <b>true</b> if the target view is available; returns  <b> false</b> otherwise.
245      * @since 1.0
246      * @version 1.0
247      */
248     static bool FindSubView(const UIView& parentView, const UIView* subView);
249 
250 #if ENABLE_WINDOW
251     /**
252      * @brief Obtains the window bound with a <b>RootView</b>.
253      *
254      * @return Returns the window.
255      * @since 1.0
256      * @version 1.0
257      */
258     Window* GetBoundWindow() const;
259 #endif
260 
261     void DrawTop(UIView* view, const Rect& rect);
262 
263     /**
264      * @brief Measure all child view under RootView
265      */
266     void Measure();
267 
268     void MeasureView(UIView* view);
269 
270     /**
271      * @brief update momory info based on FB buffer info.
272      *
273      * @param fbBufferInfo FB buffer info
274      * @since 6.0
275      * @version 6.0
276      */
277     void UpdateBufferInfo(BufferInfo* fbBufferInfo);
278 
279     /**
280      * @brief save the drawing context.
281      *
282      * @since 6.0
283      * @version 6.0
284      */
285     void SaveDrawContext();
286 
287     /**
288      * @brief restore the drawing context.
289      *
290      * @since 6.0
291      * @version 6.0
292      */
293     void RestoreDrawContext();
294 private:
295     friend class RenderManager;
296     friend class UIViewGroup;
297     friend class UIView;
298 #if ENABLE_WINDOW
299     friend class WindowImpl;
300 #endif
301 
302     RootView();
303 
304     ~RootView();
305 
306     inline Rect GetScreenRect();
307     void AddInvalidateRectWithLock(Rect& rect, UIView *view);
308     void AddInvalidateRect(Rect& rect, UIView* view);
309     void Render();
310     UIView* GetTopUIView(const Rect& rect);
311     void InitDrawContext();
312     void DestroyDrawContext();
313     void InitMapBufferInfo(BufferInfo* bufferInfo);
314     void DestroyMapBufferInfo();
315     void BlitMapBuffer(Rect& curViewRect, TransformMap& transMap, const Rect& invalidatedArea);
316     void ClearMapBuffer();
317     void UpdateMapBufferInfo(Rect& invalidatedArea);
318     void RestoreMapBufferInfo();
319 #if LOCAL_RENDER
320     void RemoveViewFromInvalidMap(UIView *view);
321     void DrawInvalidMap(const Rect &buffRect);
322     void OptimizeInvalidView(UIView* curview, UIView* backgroud, List<Rect> &renderedRects);
323     void OptimizeInvalidMap();
324 
325     std::map<UIView*, Graphic::Vector<Rect>> invalidateMap_;
326 #else
327     void OptimizeAddRect(Rect& rect);
328     void OptimizeInvalidateRects();
329     List<Rect> invalidateRects_;
330 #endif
331 
332     OnKeyActListener* onKeyActListener_ {nullptr};
333     OnVirtualDeviceEventListener* onVirtualEventListener_ {nullptr};
334 #if defined __linux__ || defined __LITEOS__ || defined __APPLE__
335     pthread_mutex_t lock_;
336 #endif
337 
338 #if ENABLE_WINDOW
339     WindowImpl* boundWindow_ {nullptr};
340 #endif
341     /**
342      * @brief draw context info.
343      * @param bufferInfo dst drawing buffer info.
344      * @param mapBufferInfo dst animator drawing buffer info.
345      * @since 6.0
346      * @version 6.0
347      */
348     struct DrawContext {
349         BufferInfo* bufferInfo;
350         BufferInfo* mapBufferInfo;
351     };
352     DrawContext dc_;
353     DrawContext bakDc_;
354 };
355 } // namespace OHOS
356 #endif // GRAPHIC_LITE_ROOT_VIEW_H
357