[dali_1.0.8] Merge branch 'tizen'
[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 #include <ecore-x-types.h>
34 #include <trigger-event.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 #if defined(DEBUG_ENABLED)
46 extern Debug::Filter* gRenderSurfaceLogFilter;
47 #endif
48
49 namespace ECore
50 {
51
52 namespace
53 {
54
55 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
56
57 } // unnamed namespace
58
59 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
60                                           Any surface,
61                                           Any display,
62                                           const std::string& name,
63                                           bool isTransparent)
64 : RenderSurface( Dali::RenderSurface::WINDOW, positionSize, surface, display, name, isTransparent ),
65   mNeedToApproveDeiconify(false)
66 {
67   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
68   Init( surface );
69 }
70
71 WindowRenderSurface::~WindowRenderSurface()
72 {
73   if( mOwnSurface )
74   {
75     ecore_x_window_free( mX11Window );
76   }
77 }
78
79 Ecore_X_Drawable WindowRenderSurface::GetDrawable()
80 {
81   // already an e-core type
82   return (Ecore_X_Drawable)mX11Window;
83 }
84
85 Dali::RenderSurface::SurfaceType WindowRenderSurface::GetType()
86 {
87   return Dali::RenderSurface::WINDOW;
88 }
89
90 Any WindowRenderSurface::GetSurface()
91 {
92   // already an e-core type
93   return Any( mX11Window );
94 }
95
96 Ecore_X_Window WindowRenderSurface::GetXWindow()
97 {
98   return mX11Window;
99 }
100
101 void WindowRenderSurface::RequestToApproveDeiconify()
102 {
103   mNeedToApproveDeiconify = true;
104 }
105
106 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
107 {
108   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
109
110   EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
111   eglImpl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
112
113   eglImpl.ChooseConfig(true, mColorDepth);
114 }
115
116 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
117 {
118   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
119
120   EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
121
122   // create the EGL surface
123   // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
124   XWindow window = static_cast< XWindow>( mX11Window );
125   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)window, mColorDepth ); // reinterpret_cast does not compile
126 }
127
128 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
129 {
130   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
131
132   EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
133   eglImpl.DestroySurface();
134 }
135
136 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& eglIf )
137 {
138   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
139
140   EglImplementation& egl = static_cast<EglImplementation&>( eglIf );
141   egl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
142
143   // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
144   XWindow window = static_cast< XWindow >( mX11Window );
145   return egl.ReplaceSurfaceWindow( (EGLNativeWindowType)window, // reinterpret_cast does not compile
146                                    reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
147 }
148
149 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
150 {
151   bool needToMove = false;
152   bool needToResize = false;
153
154   // check moving
155   if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
156       (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
157   {
158     needToMove = true;
159   }
160
161   // check resizing
162   if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
163       (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
164   {
165     needToResize = true;
166   }
167
168   if( needToMove &&  needToResize)
169   {
170     ecore_x_window_move_resize(mX11Window, positionSize.x, positionSize.y, positionSize.width, positionSize.height);
171     mPosition = positionSize;
172   }
173   else if(needToMove)
174   {
175     ecore_x_window_move(mX11Window, positionSize.x, positionSize.y);
176     mPosition = positionSize;
177   }
178   else if (needToResize)
179   {
180     ecore_x_window_resize(mX11Window, positionSize.width, positionSize.height);
181     mPosition = positionSize;
182   }
183
184 }
185
186 void WindowRenderSurface::Map()
187 {
188   ecore_x_window_show(mX11Window);
189 }
190
191 void WindowRenderSurface::StartRender()
192 {
193 }
194
195 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
196 {
197   // nothing to do for windows
198   return true;
199 }
200
201 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, unsigned int, bool )
202 {
203   EglImplementation& eglImpl = static_cast<EglImplementation&>( egl );
204   eglImpl.SwapBuffers();
205
206   // When the window is deiconified, it approves the deiconify operation to window manager after rendering
207   if(mNeedToApproveDeiconify)
208   {
209     // SwapBuffer is desychronized. So make sure to sychronize when window is deiconified.
210     glAbstraction.Finish();
211
212 #ifndef DALI_PROFILE_UBUNTU
213     /* client sends immediately reply message using value 1 */
214     ecore_x_client_message32_send(mX11Window,
215                         ECORE_X_ATOM_E_DEICONIFY_APPROVE,
216                         ECORE_X_EVENT_MASK_WINDOW_CONFIGURE,
217                         mX11Window, 1,
218                         0, 0, 0);
219 #endif // DALI_PROFILE_UBUNTU
220
221     ecore_x_sync();
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
274   // set up etc properties to match with ecore-evas
275   char *id = NULL;
276   if( ( id = getenv("DESKTOP_STARTUP_ID") ) )
277   {
278     ecore_x_netwm_startup_id_set( mX11Window, id );
279   }
280
281   ecore_x_icccm_hints_set( mX11Window,
282                            1,                                // accepts_focus
283                            ECORE_X_WINDOW_STATE_HINT_NORMAL, // initial_state
284                            0,                                // icon_pixmap
285                            0,                                // icon_mask
286                            0,                                // icon_window
287                            0,                                // window_group
288                            0 );                              // is_urgent
289
290   // we SHOULD guarantee the x11 window was created in x server.
291   ecore_x_sync();
292 }
293
294 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
295 {
296   mX11Window = static_cast< Ecore_X_Window >( surfaceId );
297 }
298
299 void WindowRenderSurface::ReleaseLock()
300 {
301   // Nothing to do.
302 }
303
304 } // namespace ECore
305
306 } // namespace Adaptor
307
308 } // namespace Internal
309
310 } // namespace Dali