532c47f52ec443805ec75fff5e08f3395e2e339a
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / window-render-surface-ecore-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 <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   mNeedToApproveDeiconify(false)
54 {
55   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
56   Init( surface );
57 }
58
59 WindowRenderSurface::~WindowRenderSurface()
60 {
61   if( mOwnSurface )
62   {
63     ecore_wl_window_free( mWlWindow );
64   }
65 }
66
67 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
68 {
69   // already an e-core type
70   return mWlWindow;
71 }
72
73 Any WindowRenderSurface::GetSurface()
74 {
75   // already an e-core type
76   return Any( mWlWindow );
77 }
78
79 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
80 {
81   return mWlWindow;
82 }
83
84 void WindowRenderSurface::RequestToApproveDeiconify()
85 {
86   mNeedToApproveDeiconify = true;
87 }
88
89 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
90 {
91   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
92
93   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
94
95   eglImpl.ChooseConfig(true, mColorDepth);
96 }
97
98 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
99 {
100   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
101
102   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
103
104   // Temporary code for opaque window. We have to modify it after wayland team finish the work.
105   if( mColorDepth == COLOR_DEPTH_32 )
106   {
107     ecore_wl_window_alpha_set( mWlWindow, true );
108   }
109   else
110   {
111     ecore_wl_window_alpha_set( mWlWindow, false );
112   }
113
114   // create the EGL surface
115   ecore_wl_window_surface_create(mWlWindow);
116   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
117   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mEglWindow, mColorDepth ); // reinterpret_cast does not compile
118 }
119
120 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
121 {
122   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
123
124   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
125   eglImpl.DestroySurface();
126   wl_egl_window_destroy(mEglWindow);
127   mEglWindow = NULL;
128 }
129
130 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
131 {
132   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
133
134   if( mEglWindow != NULL )
135   {
136     wl_egl_window_destroy(mEglWindow);
137     mEglWindow = NULL;
138   }
139
140   // Temporary code for opaque window. We have to modify it after wayland team finish the work.
141   if( mColorDepth == COLOR_DEPTH_32 )
142   {
143     ecore_wl_window_alpha_set( mWlWindow, true );
144   }
145   else
146   {
147     ecore_wl_window_alpha_set( mWlWindow, false );
148   }
149
150   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
151
152   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
153   return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mEglWindow ); // reinterpret_cast does not compile
154 }
155
156 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
157 {
158   bool needToMove = false;
159   bool needToResize = false;
160
161   // check moving
162   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
163       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
164   {
165     needToMove = true;
166   }
167
168   // check resizing
169   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
170       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
171   {
172     needToResize = true;
173   }
174
175   if(needToMove)
176   {
177     ecore_wl_window_move(mWlWindow, positionSize.x, positionSize.y);
178     mPosition = positionSize;
179   }
180   if (needToResize)
181   {
182     ecore_wl_window_resize(mWlWindow, positionSize.width, positionSize.height, 0);
183     mPosition = positionSize;
184   }
185
186 }
187
188 void WindowRenderSurface::Map()
189 {
190   ecore_wl_window_show(mWlWindow);
191 }
192
193 void WindowRenderSurface::StartRender()
194 {
195 }
196
197 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
198 {
199   // nothing to do for windows
200   return true;
201 }
202
203 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
204 {
205   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
206   eglImpl.SwapBuffers();
207
208   // When the window is deiconified, it approves the deiconify operation to window manager after rendering
209   if(mNeedToApproveDeiconify)
210   {
211     // SwapBuffer is desychronized. So make sure to sychronize when window is deiconified.
212     glAbstraction.Finish();
213
214     //FIXME
215
216     mNeedToApproveDeiconify = false;
217   }
218 }
219
220 void WindowRenderSurface::StopRender()
221 {
222 }
223
224 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
225 {
226   //FIXME
227 }
228
229 void WindowRenderSurface::CreateWlRenderable()
230 {
231    // if width or height are zero, go full screen.
232   if ( (mPosition.width == 0) || (mPosition.height == 0) )
233   {
234     // Default window size == screen size
235     mPosition.x = 0;
236     mPosition.y = 0;
237
238     ecore_wl_screen_size_get( &mPosition.width, &mPosition.height );
239   }
240
241   mWlWindow = ecore_wl_window_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
242
243   if ( mWlWindow == 0 )
244   {
245       DALI_ASSERT_ALWAYS(0 && "Failed to create X window");
246   }
247
248   //FIXME
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