2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "window-render-surface.h"
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
26 #include <X11/extensions/Xfixes.h> // for damage notify
27 #include <X11/extensions/Xdamage.h> // for damage notify
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/integration-api/debug.h>
34 #include <ecore-x-types.h>
35 #include <trigger-event.h>
36 #include <gl/egl-implementation.h>
37 #include <base/display-connection.h>
42 #if defined(DEBUG_ENABLED)
43 extern Debug::Filter* gRenderSurfaceLogFilter;
52 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
54 } // unnamed namespace
56 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
58 const std::string& name,
60 : EcoreXRenderSurface( positionSize, surface, name, isTransparent ),
61 mNeedToApproveDeiconify(false)
63 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
67 WindowRenderSurface::~WindowRenderSurface()
71 ecore_x_window_free( mX11Window );
75 Ecore_X_Drawable WindowRenderSurface::GetDrawable()
77 // already an e-core type
78 return (Ecore_X_Drawable)mX11Window;
81 Any WindowRenderSurface::GetSurface()
83 // already an e-core type
84 return Any( mX11Window );
87 Ecore_X_Window WindowRenderSurface::GetXWindow()
92 void WindowRenderSurface::RequestToApproveDeiconify()
94 mNeedToApproveDeiconify = true;
97 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
99 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
101 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
103 eglImpl.ChooseConfig(true, mColorDepth);
106 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
108 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
110 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
112 // create the EGL surface
113 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
114 XWindow window = static_cast< XWindow>( mX11Window );
115 eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)window, mColorDepth ); // reinterpret_cast does not compile
118 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
120 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
122 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
123 eglImpl.DestroySurface();
126 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
128 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
130 // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
131 XWindow window = static_cast< XWindow >( mX11Window );
132 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
134 return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)window ); // reinterpret_cast does not compile
137 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
139 bool needToMove = false;
140 bool needToResize = false;
143 if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
144 (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
150 if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
151 (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
156 if( needToMove && needToResize)
158 ecore_x_window_move_resize(mX11Window, positionSize.x, positionSize.y, positionSize.width, positionSize.height);
159 mPosition = positionSize;
163 ecore_x_window_move(mX11Window, positionSize.x, positionSize.y);
164 mPosition = positionSize;
166 else if (needToResize)
168 ecore_x_window_resize(mX11Window, positionSize.width, positionSize.height);
169 mPosition = positionSize;
174 void WindowRenderSurface::Map()
176 ecore_x_window_show(mX11Window);
179 void WindowRenderSurface::StartRender()
183 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
185 // nothing to do for windows
189 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, unsigned int deltaTime, bool replacingSurface )
191 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
192 eglImpl.SwapBuffers();
194 // When the window is deiconified, it approves the deiconify operation to window manager after rendering
195 if(mNeedToApproveDeiconify)
197 // SwapBuffer is desychronized. So make sure to sychronize when window is deiconified.
198 glAbstraction.Finish();
200 XDisplay* display = AnyCast<XDisplay *>(displayConnection->GetDisplay());
202 #ifndef DALI_PROFILE_UBUNTU
203 /* client sends immediately reply message using value 1 */
206 xev.xclient.window = mX11Window;
207 xev.xclient.type = ClientMessage;
208 xev.xclient.message_type = ECORE_X_ATOM_E_DEICONIFY_APPROVE;
209 xev.xclient.format = 32;
210 xev.xclient.data.l[0] = mX11Window;
211 xev.xclient.data.l[1] = 1;
212 xev.xclient.data.l[2] = 0;
213 xev.xclient.data.l[3] = 0;
214 xev.xclient.data.l[4] = 0;
216 XSendEvent(display, mX11Window, false, ECORE_X_EVENT_MASK_WINDOW_CONFIGURE, &xev);
217 #endif // DALI_PROFILE_UBUNTU
219 XSync(display, false);
221 mNeedToApproveDeiconify = false;
225 void WindowRenderSurface::StopRender()
229 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
231 Ecore_X_Atom viewModeAtom( ecore_x_atom_get( "_E_COMP_3D_APP_WIN" ) );
233 if( viewModeAtom != None )
235 unsigned int value( static_cast<unsigned int>( viewMode ) );
236 ecore_x_window_prop_card32_set( mX11Window, viewModeAtom, &value, 1 );
240 void WindowRenderSurface::CreateXRenderable()
242 // if width or height are zero, go full screen.
243 if ( (mPosition.width == 0) || (mPosition.height == 0) )
245 // Default window size == screen size
249 ecore_x_screen_size_get( ecore_x_default_screen_get(), &mPosition.width, &mPosition.height );
252 if(mColorDepth == COLOR_DEPTH_32)
254 // create 32 bit window
255 mX11Window = ecore_x_window_argb_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height );
259 // create 24 bit window
260 mX11Window = ecore_x_window_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height );
263 if ( mX11Window == 0 )
265 DALI_ASSERT_ALWAYS(0 && "Failed to create X window");
268 // set up window title which will be helpful for debug utitilty
269 ecore_x_icccm_title_set( mX11Window, mTitle.c_str() );
270 ecore_x_netwm_name_set( mX11Window, mTitle.c_str() );
272 // set up etc properties to match with ecore-evas
274 if( ( id = getenv("DESKTOP_STARTUP_ID") ) )
276 ecore_x_netwm_startup_id_set( mX11Window, id );
279 ecore_x_icccm_hints_set( mX11Window,
281 ECORE_X_WINDOW_STATE_HINT_NORMAL, // initial_state
288 // we SHOULD guarantee the x11 window was created in x server.
292 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
294 mX11Window = static_cast< Ecore_X_Window >( surfaceId );
297 void WindowRenderSurface::ReleaseLock()