Merge "[3.0] Update doxygen comments" into tizen_3.0
[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   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   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mEglWindow, mColorDepth ); // reinterpret_cast does not compile
120 }
121
122 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
123 {
124   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
125
126   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
127   eglImpl.DestroySurface();
128
129   if( mEglWindow != NULL )
130   {
131     wl_egl_window_destroy(mEglWindow);
132     mEglWindow = NULL;
133   }
134 }
135
136 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
137 {
138   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
139
140   if( mEglWindow != NULL )
141   {
142     wl_egl_window_destroy(mEglWindow);
143     mEglWindow = NULL;
144   }
145
146   // Temporary code for opaque window. We have to modify it after wayland team finish the work.
147   if( mColorDepth == COLOR_DEPTH_32 )
148   {
149     ecore_wl_window_alpha_set( mWlWindow, true );
150   }
151   else
152   {
153     ecore_wl_window_alpha_set( mWlWindow, false );
154   }
155
156   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
157
158   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
159   return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mEglWindow ); // reinterpret_cast does not compile
160 }
161
162 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
163 {
164   bool needToMove = false;
165   bool needToResize = false;
166
167   // check moving
168   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
169       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
170   {
171     needToMove = true;
172   }
173
174   // check resizing
175   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
176       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
177   {
178     needToResize = true;
179   }
180
181   if(needToMove)
182   {
183     ecore_wl_window_move(mWlWindow, positionSize.x, positionSize.y);
184     mPosition = positionSize;
185   }
186   if (needToResize)
187   {
188     ecore_wl_window_resize(mWlWindow, positionSize.width, positionSize.height, 0);
189     mPosition = positionSize;
190   }
191
192 }
193
194 void WindowRenderSurface::Map()
195 {
196   ecore_wl_window_show(mWlWindow);
197 }
198
199 void WindowRenderSurface::StartRender()
200 {
201 }
202
203 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
204 {
205   // nothing to do for windows
206   return true;
207 }
208
209 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
210 {
211   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
212   eglImpl.SwapBuffers();
213
214   if( mRenderNotification )
215   {
216     mRenderNotification->Trigger();
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
249 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
250 {
251   mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
252 }
253
254 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& /* threadSynchronization */ )
255 {
256   // Nothing to do.
257 }
258
259 void WindowRenderSurface::ReleaseLock()
260 {
261   // Nothing to do.
262 }
263
264 } // namespace ECore
265
266 } // namespace Dali