Merge "Fix for thread contention issues in FrameTime object." into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / window-render-surface-x.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 <X11/Xatom.h>
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25
26 #include <X11/extensions/Xfixes.h> // for damage notify
27 #include <X11/extensions/Xdamage.h> // for damage notify
28
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL INCLUDES
33
34 #include <ecore-x-types.h>
35 #include <trigger-event.h>
36 #include <gl/egl-implementation.h>
37 #include <base/display-connection.h>
38
39 namespace Dali
40 {
41
42 #if defined(DEBUG_ENABLED)
43 extern Debug::Filter* gRenderSurfaceLogFilter;
44 #endif
45
46 namespace ECore
47 {
48
49 namespace
50 {
51
52 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
53
54 } // unnamed namespace
55
56 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
57                                           Any surface,
58                                           const std::string& name,
59                                           const std::string& className,
60                                           bool isTransparent)
61 : EcoreXRenderSurface( positionSize, surface, name, isTransparent ),
62   mNeedToApproveDeiconify(false),
63   mClassName(className)
64 {
65   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
66   Init( surface );
67 }
68
69 WindowRenderSurface::~WindowRenderSurface()
70 {
71   if( mOwnSurface )
72   {
73     ecore_x_window_free( mX11Window );
74   }
75 }
76
77 Ecore_X_Drawable WindowRenderSurface::GetDrawable()
78 {
79   // already an e-core type
80   return (Ecore_X_Drawable)mX11Window;
81 }
82
83 Any WindowRenderSurface::GetSurface()
84 {
85   // already an e-core type
86   return Any( mX11Window );
87 }
88
89 Ecore_X_Window WindowRenderSurface::GetXWindow()
90 {
91   return mX11Window;
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   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
104
105   eglImpl.ChooseConfig(true, mColorDepth);
106 }
107
108 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
109 {
110   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
111
112   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
113
114   // create the EGL surface
115   // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
116   XWindow window = static_cast< XWindow>( mX11Window );
117   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)window, 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 }
127
128 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
129 {
130   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
131
132   // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
133   XWindow window = static_cast< XWindow >( mX11Window );
134   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
135
136   return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)window ); // reinterpret_cast does not compile
137 }
138
139 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
140 {
141   bool needToMove = false;
142   bool needToResize = false;
143
144   // check moving
145   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
146       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
147   {
148     needToMove = true;
149   }
150
151   // check resizing
152   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
153       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
154   {
155     needToResize = true;
156   }
157
158   if( needToMove &&  needToResize)
159   {
160     ecore_x_window_move_resize(mX11Window, positionSize.x, positionSize.y, positionSize.width, positionSize.height);
161     mPosition = positionSize;
162   }
163   else if(needToMove)
164   {
165     ecore_x_window_move(mX11Window, positionSize.x, positionSize.y);
166     mPosition = positionSize;
167   }
168   else if (needToResize)
169   {
170     ecore_x_window_resize(mX11Window, positionSize.width, positionSize.height);
171     mPosition = positionSize;
172   }
173
174 }
175
176 void WindowRenderSurface::Map()
177 {
178   ecore_x_window_show(mX11Window);
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, DisplayConnection* displayConnection, bool replacingSurface )
192 {
193   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::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     XDisplay* display = AnyCast<XDisplay *>(displayConnection->GetDisplay());
203
204 #ifndef DALI_PROFILE_UBUNTU
205     /* client sends immediately reply message using value 1 */
206     XEvent xev;
207
208     xev.xclient.window = mX11Window;
209     xev.xclient.type = ClientMessage;
210     xev.xclient.message_type = ECORE_X_ATOM_E_DEICONIFY_APPROVE;
211     xev.xclient.format = 32;
212     xev.xclient.data.l[0] = mX11Window;
213     xev.xclient.data.l[1] = 1;
214     xev.xclient.data.l[2] = 0;
215     xev.xclient.data.l[3] = 0;
216     xev.xclient.data.l[4] = 0;
217
218     XSendEvent(display, mX11Window, false, ECORE_X_EVENT_MASK_WINDOW_CONFIGURE, &xev);
219 #endif // DALI_PROFILE_UBUNTU
220
221     XSync(display, false);
222
223     mNeedToApproveDeiconify = false;
224   }
225 }
226
227 void WindowRenderSurface::StopRender()
228 {
229 }
230
231 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
232 {
233   Ecore_X_Atom viewModeAtom( ecore_x_atom_get( "_E_COMP_3D_APP_WIN" ) );
234
235   if( viewModeAtom != None )
236   {
237     unsigned int value( static_cast<unsigned int>( viewMode ) );
238     ecore_x_window_prop_card32_set( mX11Window, viewModeAtom, &value, 1 );
239   }
240 }
241
242 void WindowRenderSurface::CreateXRenderable()
243 {
244    // if width or height are zero, go full screen.
245   if ( (mPosition.width == 0) || (mPosition.height == 0) )
246   {
247     // Default window size == screen size
248     mPosition.x = 0;
249     mPosition.y = 0;
250
251     ecore_x_screen_size_get( ecore_x_default_screen_get(), &mPosition.width, &mPosition.height );
252   }
253
254   if(mColorDepth == COLOR_DEPTH_32)
255   {
256     // create 32 bit window
257     mX11Window = ecore_x_window_argb_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height );
258   }
259   else
260   {
261     // create 24 bit window
262     mX11Window = ecore_x_window_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height );
263   }
264
265   if ( mX11Window == 0 )
266   {
267       DALI_ASSERT_ALWAYS(0 && "Failed to create X window");
268   }
269
270   // set up window title which will be helpful for debug utitilty
271   ecore_x_icccm_title_set( mX11Window, mTitle.c_str() );
272   ecore_x_netwm_name_set( mX11Window, mTitle.c_str() );
273   ecore_x_icccm_name_class_set( mX11Window, mTitle.c_str(), mClassName.c_str() );
274
275   // set up etc properties to match with ecore-evas
276   char *id = NULL;
277   if( ( id = getenv("DESKTOP_STARTUP_ID") ) )
278   {
279     ecore_x_netwm_startup_id_set( mX11Window, id );
280   }
281
282   ecore_x_icccm_hints_set( mX11Window,
283                            1,                                // accepts_focus
284                            ECORE_X_WINDOW_STATE_HINT_NORMAL, // initial_state
285                            0,                                // icon_pixmap
286                            0,                                // icon_mask
287                            0,                                // icon_window
288                            0,                                // window_group
289                            0 );                              // is_urgent
290
291   // we SHOULD guarantee the x11 window was created in x server.
292   ecore_x_sync();
293 }
294
295 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
296 {
297   mX11Window = static_cast< Ecore_X_Window >( surfaceId );
298 }
299
300 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& /* threadSynchronization */ )
301 {
302   // Nothing to do.
303 }
304
305 void WindowRenderSurface::ReleaseLock()
306 {
307   // Nothing to do.
308 }
309
310 } // namespace ECore
311
312 } // namespace Dali