Revert "Revert "[3.0] Fix crash in wayland""
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / window-render-surface-wl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "window-render-surface.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <ecore-wl-types.h>
27 #include <trigger-event.h>
28 #include <gl/egl-implementation.h>
29 #include <base/display-connection.h>
30
31 namespace Dali
32 {
33
34 #if defined(DEBUG_ENABLED)
35 extern Debug::Filter* gRenderSurfaceLogFilter;
36 #endif
37
38 namespace ECore
39 {
40
41 namespace
42 {
43
44 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
45
46 } // unnamed namespace
47
48 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
49                                           Any surface,
50                                           const std::string& name,
51                                           bool isTransparent)
52 : EcoreWlRenderSurface( positionSize, surface, name, isTransparent ),
53   mEglWindow( NULL ),
54   mNeedToApproveDeiconify( false )
55 {
56   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
57   Init( surface );
58 }
59
60 WindowRenderSurface::~WindowRenderSurface()
61 {
62   if( mEglWindow != NULL )
63   {
64     wl_egl_window_destroy(mEglWindow);
65     mEglWindow = NULL;
66   }
67
68   if( mOwnSurface )
69   {
70     ecore_wl_window_free( mWlWindow );
71   }
72 }
73
74 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
75 {
76   // already an e-core type
77   return mWlWindow;
78 }
79
80 Any WindowRenderSurface::GetSurface()
81 {
82   // already an e-core type
83   return Any( mWlWindow );
84 }
85
86 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
87 {
88   return mWlWindow;
89 }
90
91 void WindowRenderSurface::RequestToApproveDeiconify()
92 {
93   mNeedToApproveDeiconify = true;
94 }
95
96 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
97 {
98   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
99
100   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
101
102   eglImpl.ChooseConfig(true, mColorDepth);
103 }
104
105 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
106 {
107   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
108
109   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
110
111   // create the EGL surface
112   ecore_wl_window_surface_create(mWlWindow);
113   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
114   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mEglWindow, mColorDepth ); // reinterpret_cast does not compile
115 }
116
117 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
118 {
119   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
120
121   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
122   eglImpl.DestroySurface();
123 }
124
125 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
126 {
127   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
128
129   if( mEglWindow != NULL )
130   {
131     wl_egl_window_destroy(mEglWindow);
132     mEglWindow = NULL;
133   }
134
135   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
136
137   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
138   return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mEglWindow ); // reinterpret_cast does not compile
139 }
140
141 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
142 {
143   bool needToMove = false;
144   bool needToResize = false;
145
146   // check moving
147   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
148       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
149   {
150     needToMove = true;
151   }
152
153   // check resizing
154   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
155       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
156   {
157     needToResize = true;
158   }
159
160   if(needToMove)
161   {
162     ecore_wl_window_move(mWlWindow, positionSize.x, positionSize.y);
163     mPosition = positionSize;
164   }
165   if (needToResize)
166   {
167     ecore_wl_window_resize(mWlWindow, positionSize.width, positionSize.height, 0);
168     mPosition = positionSize;
169   }
170
171 }
172
173 void WindowRenderSurface::Map()
174 {
175   ecore_wl_window_show(mWlWindow);
176 }
177
178 void WindowRenderSurface::StartRender()
179 {
180 }
181
182 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
183 {
184   // nothing to do for windows
185   return true;
186 }
187
188 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
189 {
190   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
191   eglImpl.SwapBuffers();
192
193   // When the window is deiconified, it approves the deiconify operation to window manager after rendering
194   if(mNeedToApproveDeiconify)
195   {
196     // SwapBuffer is desychronized. So make sure to sychronize when window is deiconified.
197     glAbstraction.Finish();
198
199     //FIXME
200
201     mNeedToApproveDeiconify = false;
202   }
203 }
204
205 void WindowRenderSurface::StopRender()
206 {
207 }
208
209 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
210 {
211   //FIXME
212 }
213
214 void WindowRenderSurface::CreateWlRenderable()
215 {
216    // if width or height are zero, go full screen.
217   if ( (mPosition.width == 0) || (mPosition.height == 0) )
218   {
219     // Default window size == screen size
220     mPosition.x = 0;
221     mPosition.y = 0;
222
223     ecore_wl_screen_size_get( &mPosition.width, &mPosition.height );
224   }
225
226   mWlWindow = ecore_wl_window_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
227
228   if ( mWlWindow == 0 )
229   {
230       DALI_ASSERT_ALWAYS(0 && "Failed to create X window");
231   }
232
233   //FIXME
234 }
235
236 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
237 {
238   mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
239 }
240
241 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& /* threadSynchronization */ )
242 {
243   // Nothing to do.
244 }
245
246 void WindowRenderSurface::ReleaseLock()
247 {
248   // Nothing to do.
249 }
250
251 } // namespace ECore
252
253 } // namespace Dali