Fix GlWindow when destroyed
[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) 2020 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/object/ref-object.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/adaptor-framework/window.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/graphics/gles/egl-graphics.h>
28 #include <dali/public-api/adaptor-framework/key-grab.h>
29 #include <dali/devel-api/adaptor-framework/gl-window.h>
30 #include <dali/internal/adaptor/common/adaptor-impl.h>
31 #include <dali/internal/window-system/common/event-handler.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
54   using KeyEventSignalType = Dali::GlWindow::KeyEventSignalType;
55   using TouchEventSignalType = Dali::GlWindow::TouchEventSignalType;
56   using FocusChangeSignalType = Dali::GlWindow::FocusChangeSignalType;
57   using ResizeSignalType = Dali::GlWindow::ResizeSignalType;
58   using VisibilityChangedSignalType = Dali::GlWindow::VisibilityChangedSignalType;
59   using SignalType = Signal< void () >;
60
61   /**
62    * @brief Create a new GlWindow. This should only be called once by the Application class
63    * @param[in] positionSize The position and size of the window
64    * @param[in] name The window title
65    * @param[in] className The window class name
66    * @param[in] isTransparent Whether window is transparent
67    * @return A newly allocated Window
68    */
69   static GlWindow* New( const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent = false );
70
71   /**
72    * @copydoc Dali::GlWindow::SetEglConfig()
73    */
74   void SetEglConfig( bool depth, bool stencil, int msaa, Dali::GlWindow::GlesVersion version );
75
76   /**
77    * @copydoc Dali::GlWindow::Raise()
78    */
79   void Raise();
80
81   /**
82    * @copydoc Dali::GlWindow::Lower()
83    */
84   void Lower();
85
86   /**
87    * @copydoc Dali::GlWindow::Activate()
88    */
89   void Activate();
90
91   /**
92    * @copydoc Dali::GlWindow::Show()
93    */
94   void Show();
95
96   /**
97    * @copydoc Dali::GlWindow::Hide()
98    */
99   void Hide();
100
101   /**
102    * @copydoc Dali::GlWindow::GetSupportedAuxiliaryHintCount()
103    */
104   unsigned int GetSupportedAuxiliaryHintCount() const;
105
106   /**
107    * @copydoc Dali::GlWindow::GetSupportedAuxiliaryHint()
108    */
109   std::string GetSupportedAuxiliaryHint( unsigned int index ) const;
110
111   /**
112    * @copydoc Dali::GlWindow::AddAuxiliaryHint()
113    */
114   unsigned int AddAuxiliaryHint( const std::string& hint, const std::string& value );
115
116   /**
117    * @copydoc Dali::GlWindow::RemoveAuxiliaryHint()
118    */
119   bool RemoveAuxiliaryHint( unsigned int id );
120
121   /**
122    * @copydoc Dali::GlWindow::SetAuxiliaryHintValue()
123    */
124   bool SetAuxiliaryHintValue( unsigned int id, const std::string& value );
125
126   /**
127    * @copydoc Dali::GlWindow::GetAuxiliaryHintValue()
128    */
129   std::string GetAuxiliaryHintValue( unsigned int id ) const;
130
131   /**
132    * @copydoc Dali::GlWindow::GetAuxiliaryHintId()
133    */
134   unsigned int GetAuxiliaryHintId( const std::string& hint ) const;
135
136   /**
137    * @copydoc Dali::GlWindow::SetInputRegion()
138    */
139   void SetInputRegion( const Rect< int >& inputRegion );
140
141   /**
142    * @copydoc Dali::GlWindow::SetOpaqueState()
143    */
144   void SetOpaqueState( bool opaque );
145
146   /**
147    * @copydoc Dali::GlWindow::IsOpaqueState()
148    */
149   bool IsOpaqueState() const;
150
151   /**
152    * @copydoc Dali::GlWindow::SetPositionSize()
153    */
154   void SetPositionSize( PositionSize positionSize );
155
156   /**
157    * @copydoc Dali::GlWindow::GetPositionSize()
158    */
159   PositionSize GetPositionSize() const;
160
161   /**
162    * @copydoc Dali::GlWindow::GetCurrentOrientation() const
163    */
164   Dali::GlWindow::GlWindowOrientation GetCurrentOrientation() const;
165
166   /**
167    * @copydoc Dali::GlWindow::SetAvailableOrientations()
168    */
169   void SetAvailableOrientations( const Dali::Vector< Dali::GlWindow::GlWindowOrientation >& orientations );
170
171   /**
172    * @copydoc Dali::GlWindow::SetPreferredOrientation()
173    */
174   void SetPreferredOrientation( Dali::GlWindow::GlWindowOrientation orientation );
175
176   /**
177    * @copydoc Dali::GlWindow::RegisterGlCallback()
178    */
179   void RegisterGlCallback( GlInitialize glInit, GlRenderFrame glRenderFrame, GlTerminate glTerminate );
180
181   /**
182    * @copydoc Dali::GlWindow::RenderOnce()
183    */
184   void RenderOnce();
185
186 public: // For implementation
187   /**
188    * @brief Sets child window with Dali::Window
189    *
190    * @param[in] child The child window.
191    *
192    * Most of cases, child window is the default window in adaptor
193    *
194    * Currently the child window is default window.
195    */
196   void SetChild( Dali::Window& child );
197
198 private:
199
200   /**
201    * Private constructor.
202    * @sa Window::New()
203    */
204   GlWindow();
205
206   /**
207    * Destructor
208    */
209   virtual ~GlWindow();
210
211   /**
212    * Second stage initialization
213    *
214    * @param[in] positionSize The position and size of the window
215    * @param[in] name The window title
216    * @param[in] className The window class name
217    */
218   void Initialize( const PositionSize& positionSize, const std::string& name, const std::string& className );
219
220   /**
221    * Called when the window becomes iconified or deiconified.
222    *
223    * @param[in] iconified The flag whether window is iconifed or deiconfied.
224    */
225   void OnIconifyChanged( bool iconified );
226
227   /**
228    * Called when the window focus is changed.
229    * @param[in] focusIn The flag whether window is focused or not.
230    */
231   void OnFocusChanged( bool focusIn );
232
233   /**
234    * Called when the output is transformed.
235    */
236   void OnOutputTransformed();
237
238   /**
239    * Called when the window receives a delete request.
240    */
241   void OnDeleteRequest();
242
243   /**
244    * @brief Set available rotation angle to window base.
245    *
246    * @param[in] angles The list of the avaiabled rotation angle.
247    */
248   void SetAvailableAnlges( const std::vector< int >& angles );
249
250   /**
251    * @brief Check available window orientation for Available angle.
252    *
253    * @param[in] orientation the oritation value of window rotation.
254    *
255    * @return true is available window orientation. false is not available.
256    */
257   bool IsOrientationAvailable( Dali::GlWindow::GlWindowOrientation orientation ) const;
258
259   /**
260    * @brief Convert from window orientation to angle using orientation mode value.
261    *
262    * @param[in] orientation the oritation value of window rotation.
263    *
264    * @return The coverted angle value is returned.
265    */
266   int ConvertToAngle( Dali::GlWindow::GlWindowOrientation orientation );
267
268   /**
269    * @brief Convert from angle to window orientation using orientation mode value.
270    *
271    * @param[in] angle the angle value of window rotation.
272    *
273    * @return The converted window orientation value is returned.
274    */
275   Dali::GlWindow::GlWindowOrientation ConvertToOrientation( int angle ) const;
276
277   /**
278    * @brief Run Ui GL callback function.
279    *
280    * @return true is the callback function works continuos.
281    */
282   bool RunCallback();
283
284   /**
285    * @brief Initialize and create EGL resource
286    */
287   void InitializeGraphics();
288
289   /**
290    * @brief Sets event handler for window's events.
291    */
292   void SetEventHandler() ;
293
294   /**
295    * @brief calculate touch position for rotation.
296    */
297   void RecalculateTouchPosition( Integration::Point& point ) ;
298
299   /**
300    * @brief Sets window and class name.
301    *
302    * @param[in] name The name of the window
303    * @param[in] className The class of the window
304    */
305   void SetClass( const std::string& name, const std::string className );
306
307 private:
308
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   /**
332    * @copydoc Dali::Internal::Adaptor::DamageObserver::OnDamaged()
333    */
334   void OnDamaged( const DamageArea& area );
335
336 public: // Signals
337
338   /**
339    * @copydoc Dali::GlWindow::FocusChangeSignal()
340    */
341   FocusChangeSignalType& FocusChangeSignal() { return mFocusChangeSignal; }
342
343   /**
344    * @copydoc Dali::GlWindow::ResizeSignal()
345    */
346   ResizeSignalType& ResizeSignal() { return mResizeSignal; }
347
348   /**
349    * @copydoc Dali::GlWindow::KeyEventSignal()
350    */
351   KeyEventSignalType& KeyEventSignal() { return mKeyEventSignal; }
352
353   /**
354    * @copydoc Dali::GlWindow::TouchSignal()
355    */
356   TouchEventSignalType& TouchedSignal() { return mTouchedSignal; }
357
358   /**
359    * @copydoc Dali::GlWindow::VisibilityChangedSignal()
360    */
361   VisibilityChangedSignalType& VisibilityChangedSignal() { return mVisibilityChangedSignal; }
362
363 private:
364
365   std::unique_ptr< WindowBase >               mWindowBase;
366   GraphicsInterface*                          mGraphics;                    ///< Graphics interface
367   std::string                                 mName;
368   std::string                                 mClassName;
369   EventHandlerPtr                             mEventHandler;         ///< The window events handler
370   PositionSize                                mPositionSize;
371   ColorDepth                                  mColorDepth;
372   Dali::Window                                mChildWindow;
373   bool                                        mIsTransparent:1;
374   bool                                        mIsFocusAcceptable:1;
375   bool                                        mIconified:1;
376   bool                                        mOpaqueState:1;
377   bool                                        mResizeEnabled:1;
378   bool                                        mVisible:1;
379   bool                                        mIsRotated:1;
380   bool                                        mIsWindowRotated:1;
381   bool                                        mIsTouched:1;
382
383   std::vector< int >                          mAvailableAngles;
384   int                                         mPreferredAngle;
385   int                                         mTotalRotationAngle;   ///< The angle of window + screen rotation angle % 360
386   int                                         mWindowRotationAngle;  ///< The angle of window rotation angle
387   int                                         mScreenRotationAngle;  ///< The angle of screen rotation angle
388   int                                         mOrientationMode;      ///< 0: Default portrati, 1:Default landscape
389   int                                         mWindowWidth;          ///< The width of the window
390   int                                         mWindowHeight;         ///< The height of the window
391   int                                         mNativeWindowId;       ///< The Native Window Id
392
393   // Signals
394   KeyEventSignalType                          mKeyEventSignal;
395   TouchEventSignalType                        mTouchedSignal;
396   FocusChangeSignalType                       mFocusChangeSignal;
397   ResizeSignalType                            mResizeSignal;
398   VisibilityChangedSignalType                 mVisibilityChangedSignal;
399
400   // EGL, GL Resource
401   GlInitialize                                mGLInitCallback;
402   GlRenderFrame                               mGLRenderFrameCallback;
403   GlTerminate                                 mGLTerminateCallback;
404   CallbackBase*                               mGLRenderCallback;
405   EGLSurface                                  mEGLSurface;
406   EGLContext                                  mEGLContext;
407   Dali::GlWindow::GlesVersion                 mGLESVersion;
408   bool                                        mInitCallback:1;
409   bool                                        mDepth:1;
410   bool                                        mStencil:1;
411   bool                                        mIsEGLInitialize:1;
412   int                                         mMSAA;
413 };
414
415 } // namespace Adaptor
416 } // namepsace Internal
417
418 // Helpers for public-api forwarding methods
419
420 inline Internal::Adaptor::GlWindow& GetImplementation(Dali::GlWindow& window)
421 {
422   DALI_ASSERT_ALWAYS( window && "Window handle is empty" );
423   BaseObject& object = window.GetBaseObject();
424   return static_cast<Internal::Adaptor::GlWindow&>(object);
425 }
426
427 inline const Internal::Adaptor::GlWindow& GetImplementation(const Dali::GlWindow& window)
428 {
429   DALI_ASSERT_ALWAYS( window && "Window handle is empty" );
430   const BaseObject& object = window.GetBaseObject();
431   return static_cast<const Internal::Adaptor::GlWindow&>(object);
432 }
433
434 } // namespace Dali
435
436 #endif // DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_IMPL_H