fix window resize and roation issue in GlWindow
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / gl-window-impl.h
1 #ifndef DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_IMPL_H
2 #define DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_IMPL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/window.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/object/ref-object.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/devel-api/adaptor-framework/gl-window.h>
28 #include <dali/internal/adaptor/common/adaptor-impl.h>
29 #include <dali/internal/graphics/gles/egl-graphics.h>
30 #include <dali/internal/window-system/common/event-handler.h>
31 #include <dali/internal/window-system/common/gl-window-render-thread.h>
32
33 namespace Dali
34 {
35 class Adaptor;
36
37 namespace Internal
38 {
39 namespace Adaptor
40 {
41 class WindowBase;
42
43 class GlWindow;
44 using GlWindowPtr     = IntrusivePtr<GlWindow>;
45 using EventHandlerPtr = IntrusivePtr<EventHandler>;
46
47 /**
48  * Window provides a surface to render onto with orientation.
49  */
50 class GlWindow : public BaseObject, public EventHandler::Observer, public DamageObserver, public ConnectionTracker
51 {
52 public:
53   using KeyEventSignalType          = Dali::GlWindow::KeyEventSignalType;
54   using TouchEventSignalType        = Dali::GlWindow::TouchEventSignalType;
55   using FocusChangeSignalType       = Dali::GlWindow::FocusChangeSignalType;
56   using ResizeSignalType            = Dali::GlWindow::ResizeSignalType;
57   using VisibilityChangedSignalType = Dali::GlWindow::VisibilityChangedSignalType;
58   using SignalType                  = Signal<void()>;
59
60   /**
61    * @brief Create a new GlWindow. This should only be called once by the Application class
62    * @param[in] positionSize The position and size of the window
63    * @param[in] name The window title
64    * @param[in] className The window class name
65    * @param[in] isTransparent Whether window is transparent
66    * @return A newly allocated Window
67    */
68   static GlWindow* New(const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent = false);
69
70   /**
71    * @copydoc Dali::GlWindow::SetEglConfig()
72    */
73   void SetEglConfig(bool depth, bool stencil, int msaa, Dali::GlWindow::GlesVersion version);
74
75   /**
76    * @copydoc Dali::GlWindow::Raise()
77    */
78   void Raise();
79
80   /**
81    * @copydoc Dali::GlWindow::Lower()
82    */
83   void Lower();
84
85   /**
86    * @copydoc Dali::GlWindow::Activate()
87    */
88   void Activate();
89
90   /**
91    * @copydoc Dali::GlWindow::Show()
92    */
93   void Show();
94
95   /**
96    * @copydoc Dali::GlWindow::Hide()
97    */
98   void Hide();
99
100   /**
101    * @copydoc Dali::GlWindow::GetSupportedAuxiliaryHintCount()
102    */
103   unsigned int GetSupportedAuxiliaryHintCount() const;
104
105   /**
106    * @copydoc Dali::GlWindow::GetSupportedAuxiliaryHint()
107    */
108   std::string GetSupportedAuxiliaryHint(unsigned int index) const;
109
110   /**
111    * @copydoc Dali::GlWindow::AddAuxiliaryHint()
112    */
113   unsigned int AddAuxiliaryHint(const std::string& hint, const std::string& value);
114
115   /**
116    * @copydoc Dali::GlWindow::RemoveAuxiliaryHint()
117    */
118   bool RemoveAuxiliaryHint(unsigned int id);
119
120   /**
121    * @copydoc Dali::GlWindow::SetAuxiliaryHintValue()
122    */
123   bool SetAuxiliaryHintValue(unsigned int id, const std::string& value);
124
125   /**
126    * @copydoc Dali::GlWindow::GetAuxiliaryHintValue()
127    */
128   std::string GetAuxiliaryHintValue(unsigned int id) const;
129
130   /**
131    * @copydoc Dali::GlWindow::GetAuxiliaryHintId()
132    */
133   unsigned int GetAuxiliaryHintId(const std::string& hint) const;
134
135   /**
136    * @copydoc Dali::GlWindow::SetInputRegion()
137    */
138   void SetInputRegion(const Rect<int>& inputRegion);
139
140   /**
141    * @copydoc Dali::GlWindow::SetOpaqueState()
142    */
143   void SetOpaqueState(bool opaque);
144
145   /**
146    * @copydoc Dali::GlWindow::IsOpaqueState()
147    */
148   bool IsOpaqueState() const;
149
150   /**
151    * @copydoc Dali::GlWindow::SetPositionSize()
152    */
153   void SetPositionSize(PositionSize positionSize);
154
155   /**
156    * @copydoc Dali::GlWindow::GetPositionSize()
157    */
158   PositionSize GetPositionSize() const;
159
160   /**
161    * @copydoc Dali::GlWindow::GetCurrentOrientation() const
162    */
163   WindowOrientation GetCurrentOrientation() const;
164
165   /**
166    * @copydoc Dali::GlWindow::SetAvailableOrientations()
167    */
168   void SetAvailableOrientations(const Dali::Vector<WindowOrientation>& orientations);
169
170   /**
171    * @copydoc Dali::GlWindow::SetPreferredOrientation()
172    */
173   void SetPreferredOrientation(WindowOrientation orientation);
174
175   /**
176    * @copydoc Dali::GlWindow::RegisterGlCallback()
177    */
178   void RegisterGlCallback(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
179
180   /**
181    * @copydoc Dali::GlWindow::RenderOnce()
182    */
183   void RenderOnce();
184
185   /**
186    * @copydoc Dali::GlWindow::SetRenderingMode()
187    */
188   void SetRenderingMode(Dali::GlWindow::RenderingMode mode);
189
190   /**
191    * @copydoc Dali::GlWindow::GetRenderingMode()
192    */
193   Dali::GlWindow::RenderingMode GetRenderingMode() const;
194
195 public: // For implementation
196   /**
197    * @brief Sets child window with Dali::Window
198    *
199    * @param[in] child The child window.
200    *
201    * Most of cases, child window is the default window in adaptor
202    *
203    * Currently the child window is default window.
204    */
205   void SetChild(Dali::Window& child);
206
207 private:
208   /**
209    * Private constructor.
210    * @sa Window::New()
211    */
212   GlWindow();
213
214   /**
215    * Destructor
216    */
217   virtual ~GlWindow();
218
219   /**
220    * Second stage initialization
221    *
222    * @param[in] positionSize The position and size of the window
223    * @param[in] name The window title
224    * @param[in] className The window class name
225    */
226   void Initialize(const PositionSize& positionSize, const std::string& name, const std::string& className);
227
228   /**
229    * Called when the window becomes iconified or deiconified.
230    *
231    * @param[in] iconified The flag whether window is iconifed or deiconfied.
232    */
233   void OnIconifyChanged(bool iconified);
234
235   /**
236    * Called when the window focus is changed.
237    * @param[in] focusIn The flag whether window is focused or not.
238    */
239   void OnFocusChanged(bool focusIn);
240
241   /**
242    * Called when the output is transformed.
243    */
244   void OnOutputTransformed();
245
246   /**
247    * Called when the window receives a delete request.
248    */
249   void OnDeleteRequest();
250
251   /**
252    * @brief Set available rotation angle to window base.
253    *
254    * @param[in] angles The list of the avaiabled rotation angle.
255    */
256   void SetAvailableAnlges(const std::vector<int>& angles);
257
258   /**
259    * @brief Check available window orientation for Available angle.
260    *
261    * @param[in] orientation the oritation value of window rotation.
262    *
263    * @return true is available window orientation. false is not available.
264    */
265   bool IsOrientationAvailable(WindowOrientation orientation) const;
266
267   /**
268    * @brief Convert from window orientation to angle using orientation mode value.
269    *
270    * @param[in] orientation the oritation value of window rotation.
271    *
272    * @return The coverted angle value is returned.
273    */
274   int ConvertToAngle(WindowOrientation orientation);
275
276   /**
277    * @brief Convert from angle to window orientation using orientation mode value.
278    *
279    * @param[in] angle the angle value of window rotation.
280    *
281    * @return The converted window orientation value is returned.
282    */
283   WindowOrientation ConvertToOrientation(int angle) const;
284
285   /**
286    * @brief Initialize and create EGL resource
287    */
288   void InitializeGraphics();
289
290   /**
291    * @brief Sets event handler for window's events.
292    */
293   void SetEventHandler();
294
295   /**
296    * @brief calculate touch position for rotation.
297    */
298   void RecalculateTouchPosition(Integration::Point& point);
299
300   /**
301    * @brief Sets window and class name.
302    *
303    * @param[in] name The name of the window
304    * @param[in] className The class of the window
305    */
306   void SetClass(const std::string& name, const std::string className);
307
308 private:
309   /**
310    * @copydoc Dali::Internal::Adaptor::EventHandler::Observer::OnTouchPoint
311    */
312   void OnTouchPoint(Dali::Integration::Point& point, int timeStamp) override;
313
314   /**
315    * @copydoc Dali::Internal::Adaptor::EventHandler::Observer::OnWheelEvent
316    */
317   void OnWheelEvent(Dali::Integration::WheelEvent& wheelEvent) override;
318
319   /**
320    * @copydoc Dali::Internal::Adaptor::EventHandler::Observer::OnKeyEvent
321    */
322   void OnKeyEvent(Dali::Integration::KeyEvent& keyEvent) override;
323
324   /**
325    * @copydoc Dali::Internal::Adaptor::EventHandler::Observer::OnRotation
326    */
327   void OnRotation(const RotationEvent& rotation) override;
328
329 private: // From Dali::Internal::Adaptor::DamageObserver
330   /**
331    * @copydoc Dali::Internal::Adaptor::DamageObserver::OnDamaged()
332    */
333   void OnDamaged(const DamageArea& area);
334
335   /**
336    * @brief Updates screen rotation value and screen rotation works.
337    *
338    * @param[in] newAngle new screen rotation angle
339    */
340   void UpdateScreenRotation(int newAngle);
341
342 public: // Signals
343   /**
344    * @copydoc Dali::GlWindow::FocusChangeSignal()
345    */
346   FocusChangeSignalType& FocusChangeSignal()
347   {
348     return mFocusChangeSignal;
349   }
350
351   /**
352    * @copydoc Dali::GlWindow::ResizeSignal()
353    */
354   ResizeSignalType& ResizeSignal()
355   {
356     return mResizeSignal;
357   }
358
359   /**
360    * @copydoc Dali::GlWindow::KeyEventSignal()
361    */
362   KeyEventSignalType& KeyEventSignal()
363   {
364     return mKeyEventSignal;
365   }
366
367   /**
368    * @copydoc Dali::GlWindow::TouchSignal()
369    */
370   TouchEventSignalType& TouchedSignal()
371   {
372     return mTouchedSignal;
373   }
374
375   /**
376    * @copydoc Dali::GlWindow::VisibilityChangedSignal()
377    */
378   VisibilityChangedSignalType& VisibilityChangedSignal()
379   {
380     return mVisibilityChangedSignal;
381   }
382
383 private:
384   std::unique_ptr<WindowBase>              mWindowBase;
385   std::unique_ptr<GraphicsInterface>       mGraphics;             ///< Graphics interface
386   std::unique_ptr<Dali::DisplayConnection> mDisplayConnection;    ///< The native display connection
387   std::unique_ptr<GlWindowRenderThread>    mGlWindowRenderThread; ///< The render thread
388   EventHandlerPtr                          mEventHandler;         ///< The window events handler
389   Dali::Window                             mChildWindow;          ///< The default child UI Window
390   std::string                              mName;
391   std::string                              mClassName;
392   bool                                     mIsTransparent : 1;
393   bool                                     mIsFocusAcceptable : 1;
394   bool                                     mIconified : 1;
395   bool                                     mOpaqueState : 1;
396   bool                                     mResizeEnabled : 1;
397   bool                                     mVisible : 1;
398   bool                                     mIsWindowRotated : 1;
399   bool                                     mIsTouched : 1;
400   bool                                     mIsEGLInitialized : 1;
401   bool                                     mDepth : 1;
402   bool                                     mStencil : 1;
403
404   PositionSize                  mPositionSize; ///< The window position and size
405   EnvironmentOptions            mEnvironmentOptions;
406   std::vector<int>              mAvailableAngles; ///< The list of available angle
407   ColorDepth                    mColorDepth;      ///< The color depth of window
408   Dali::GlWindow::RenderingMode mRenderingMode;   ///< The rendering mode
409
410   int mPreferredAngle;      ///< The angle of preferred angle
411   int mTotalRotationAngle;  ///< The angle of window + screen rotation angle % 360
412   int mWindowRotationAngle; ///< The angle of window rotation angle
413   int mScreenRotationAngle; ///< The angle of screen rotation angle
414   int mOrientationMode;     ///< 0: Default portrati, 1:Default landscape
415   int mWindowWidth;         ///< The width of the window
416   int mWindowHeight;        ///< The height of the window
417   int mNativeWindowId;      ///< The Native Window Id
418   int mMSAA;                ///< The multisample anti-aliasing for EGL Configuration
419
420   // Signals
421   KeyEventSignalType          mKeyEventSignal;
422   TouchEventSignalType        mTouchedSignal;
423   FocusChangeSignalType       mFocusChangeSignal;
424   ResizeSignalType            mResizeSignal;
425   VisibilityChangedSignalType mVisibilityChangedSignal;
426 };
427
428 } // namespace Adaptor
429 } // namespace Internal
430
431 // Helpers for public-api forwarding methods
432
433 inline Internal::Adaptor::GlWindow& GetImplementation(Dali::GlWindow& window)
434 {
435   DALI_ASSERT_ALWAYS(window && "Window handle is empty");
436   BaseObject& object = window.GetBaseObject();
437   return static_cast<Internal::Adaptor::GlWindow&>(object);
438 }
439
440 inline const Internal::Adaptor::GlWindow& GetImplementation(const Dali::GlWindow& window)
441 {
442   DALI_ASSERT_ALWAYS(window && "Window handle is empty");
443   const BaseObject& object = window.GetBaseObject();
444   return static_cast<const Internal::Adaptor::GlWindow&>(object);
445 }
446
447 } // namespace Dali
448
449 #endif // DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_IMPL_H