[dali_1.0.8] Merge branch 'tizen'
[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
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 #if defined(DEBUG_ENABLED)
39 extern Debug::Filter* gRenderSurfaceLogFilter;
40 #endif
41
42 namespace ECore
43 {
44
45 namespace
46 {
47
48 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
49
50 } // unnamed namespace
51
52 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
53                                           Any surface,
54                                           Any display,
55                                           const std::string& name,
56                                           bool isTransparent)
57 : RenderSurface( Dali::RenderSurface::WINDOW, positionSize, surface, display, name, isTransparent ),
58   mNeedToApproveDeiconify(false)
59 {
60   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
61   Init( surface );
62 }
63
64 WindowRenderSurface::~WindowRenderSurface()
65 {
66   if( mOwnSurface )
67   {
68     ecore_wl_window_free( mWlWindow );
69   }
70 }
71
72 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
73 {
74   // already an e-core type
75   return mWlWindow;
76 }
77
78 Dali::RenderSurface::SurfaceType WindowRenderSurface::GetType()
79 {
80   return Dali::RenderSurface::WINDOW;
81 }
82
83 Any WindowRenderSurface::GetSurface()
84 {
85   // already an e-core type
86   return Any( mWlWindow );
87 }
88
89 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
90 {
91   return mWlWindow;
92 }
93
94 void WindowRenderSurface::RequestToApproveDeiconify()
95 {
96   mNeedToApproveDeiconify = true;
97 }
98
99 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
100 {
101   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
102
103   EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
104   eglImpl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
105
106   eglImpl.ChooseConfig(true, mColorDepth);
107 }
108
109 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
110 {
111   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
112
113   EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
114
115   // create the EGL surface
116   ecore_wl_window_surface_create(mWlWindow);
117   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
118   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mEglWindow, mColorDepth ); // reinterpret_cast does not compile
119 }
120
121 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
122 {
123   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
124
125   EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
126   eglImpl.DestroySurface();
127   wl_egl_window_destroy(mEglWindow);
128   mEglWindow = NULL;
129 }
130
131 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& eglIf )
132 {
133   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
134
135   EglImplementation& egl = static_cast<EglImplementation&>( eglIf );
136   egl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
137
138   wl_egl_window_destroy(mEglWindow);
139   mEglWindow = wl_egl_window_create(ecore_wl_window_surface_get(mWlWindow), mPosition.width, mPosition.height);
140   return egl.ReplaceSurfaceWindow( (EGLNativeWindowType)mEglWindow, // reinterpret_cast does not compile
141                                    reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
142 }
143
144 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
145 {
146   bool needToMove = false;
147   bool needToResize = false;
148
149   // check moving
150   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
151       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
152   {
153     needToMove = true;
154   }
155
156   // check resizing
157   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
158       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
159   {
160     needToResize = true;
161   }
162
163   if(needToMove)
164   {
165     ecore_wl_window_move(mWlWindow, positionSize.x, positionSize.y);
166     mPosition = positionSize;
167   }
168   if (needToResize)
169   {
170     ecore_wl_window_resize(mWlWindow, positionSize.width, positionSize.height, 0);
171     mPosition = positionSize;
172   }
173
174 }
175
176 void WindowRenderSurface::Map()
177 {
178   ecore_wl_window_show(mWlWindow);
179 }
180
181 void WindowRenderSurface::StartRender()
182 {
183 }
184
185 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
186 {
187   // nothing to do for windows
188   return true;
189 }
190
191 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, unsigned int, bool )
192 {
193   EglImplementation& eglImpl = static_cast<EglImplementation&>( egl );
194   eglImpl.SwapBuffers();
195
196   // When the window is deiconified, it approves the deiconify operation to window manager after rendering
197   if(mNeedToApproveDeiconify)
198   {
199     // SwapBuffer is desychronized. So make sure to sychronize when window is deiconified.
200     glAbstraction.Finish();
201
202     mNeedToApproveDeiconify = false;
203   }
204 }
205
206 void WindowRenderSurface::StopRender()
207 {
208 }
209
210 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
211 {
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
234 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
235 {
236   mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
237 }
238
239 void WindowRenderSurface::ReleaseLock()
240 {
241   // Nothing to do.
242 }
243
244 } // namespace ECore
245
246 } // namespace Adaptor
247
248 } // namespace Internal
249
250 } // namespace Dali