[Tizen] Add key grab list API
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / window-render-surface-ecore-wl.cpp
1 /*
2  * Copyright (c) 2017 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 <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   mWlWindow( NULL ),
54   mEglWindow( NULL )
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::InitializeEgl( EglInterface& eglIf )
92 {
93   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
94
95   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
96
97   eglImpl.ChooseConfig(true, mColorDepth);
98 }
99
100 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
101 {
102   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
103
104   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
105
106   // Temporary code for opaque window. We have to modify it after wayland team finish the work.
107   if( mColorDepth == COLOR_DEPTH_32 )
108   {
109     ecore_wl_window_alpha_set( mWlWindow, true );
110   }
111   else
112   {
113     ecore_wl_window_alpha_set( mWlWindow, false );
114   }
115
116   // create the EGL surface
117   ecore_wl_window_surface_create(mWlWindow);
118   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
119   EGLNativeWindowType windowType( mEglWindow );
120   eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
121 }
122
123 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
124 {
125   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
126
127   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
128   eglImpl.DestroySurface();
129
130   if( mEglWindow != NULL )
131   {
132     wl_egl_window_destroy(mEglWindow);
133     mEglWindow = NULL;
134   }
135 }
136
137 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
138 {
139   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
140
141   if( mEglWindow != NULL )
142   {
143     wl_egl_window_destroy(mEglWindow);
144     mEglWindow = NULL;
145   }
146
147   // Temporary code for opaque window. We have to modify it after wayland team finish the work.
148   if( mColorDepth == COLOR_DEPTH_32 )
149   {
150     ecore_wl_window_alpha_set( mWlWindow, true );
151   }
152   else
153   {
154     ecore_wl_window_alpha_set( mWlWindow, false );
155   }
156
157   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
158
159   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
160   EGLNativeWindowType windowType( mEglWindow );
161   return eglImpl.ReplaceSurfaceWindow( windowType );
162 }
163
164 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
165 {
166   bool needToMove = false;
167   bool needToResize = false;
168
169   // check moving
170   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
171       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
172   {
173     needToMove = true;
174   }
175
176   // check resizing
177   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
178       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
179   {
180     needToResize = true;
181   }
182
183   if(needToMove)
184   {
185     ecore_wl_window_move(mWlWindow, positionSize.x, positionSize.y);
186     mPosition = positionSize;
187   }
188   if (needToResize)
189   {
190     ecore_wl_window_resize(mWlWindow, positionSize.width, positionSize.height, 0);
191     mPosition = positionSize;
192   }
193
194 }
195
196 void WindowRenderSurface::Map()
197 {
198   ecore_wl_window_show(mWlWindow);
199 }
200
201 void WindowRenderSurface::StartRender()
202 {
203 }
204
205 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
206 {
207   // nothing to do for windows
208   return true;
209 }
210
211 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
212 {
213   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
214   eglImpl.SwapBuffers();
215
216   if( mRenderNotification )
217   {
218     mRenderNotification->Trigger();
219   }
220 }
221
222 void WindowRenderSurface::StopRender()
223 {
224 }
225
226 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
227 {
228   //FIXME
229 }
230
231 void WindowRenderSurface::CreateWlRenderable()
232 {
233    // if width or height are zero, go full screen.
234   if ( (mPosition.width == 0) || (mPosition.height == 0) )
235   {
236     // Default window size == screen size
237     mPosition.x = 0;
238     mPosition.y = 0;
239
240     ecore_wl_screen_size_get( &mPosition.width, &mPosition.height );
241   }
242
243   mWlWindow = ecore_wl_window_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
244
245   if ( mWlWindow == 0 )
246   {
247     DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
248   }
249 }
250
251 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
252 {
253   mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
254 }
255
256 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& /* threadSynchronization */ )
257 {
258   // Nothing to do.
259 }
260
261 void WindowRenderSurface::ReleaseLock()
262 {
263   // Nothing to do.
264 }
265
266 } // namespace ECore
267
268 } // namespace Dali