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>
33 #include <ecore-x-types.h>
34 #include <trigger-event.h>
45 #if defined(DEBUG_ENABLED)
46 extern Debug::Filter* gRenderSurfaceLogFilter;
55 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
57 } // unnamed namespace
59 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
62 const std::string& name,
64 : RenderSurface( Dali::RenderSurface::WINDOW, positionSize, surface, display, name, isTransparent ),
65 mNeedToApproveDeiconify(false)
67 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
71 WindowRenderSurface::~WindowRenderSurface()
75 ecore_x_window_free( mX11Window );
79 Ecore_X_Drawable WindowRenderSurface::GetDrawable()
81 // already an e-core type
82 return (Ecore_X_Drawable)mX11Window;
85 Dali::RenderSurface::SurfaceType WindowRenderSurface::GetType()
87 return Dali::RenderSurface::WINDOW;
90 Any WindowRenderSurface::GetSurface()
92 // already an e-core type
93 return Any( mX11Window );
96 Ecore_X_Window WindowRenderSurface::GetXWindow()
101 void WindowRenderSurface::RequestToApproveDeiconify()
103 mNeedToApproveDeiconify = true;
106 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
108 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
110 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
111 eglImpl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
113 eglImpl.ChooseConfig(true, mColorDepth);
116 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
118 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
120 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
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
128 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
130 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
132 EglImplementation& eglImpl = static_cast<EglImplementation&>( eglIf );
133 eglImpl.DestroySurface();
136 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& eglIf )
138 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
140 EglImplementation& egl = static_cast<EglImplementation&>( eglIf );
141 egl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ) );
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 ) );
149 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
151 bool needToMove = false;
152 bool needToResize = false;
155 if( (fabs(positionSize.x - mPosition.x) > MINIMUM_DIMENSION_CHANGE) ||
156 (fabs(positionSize.y - mPosition.y) > MINIMUM_DIMENSION_CHANGE) )
162 if( (fabs(positionSize.width - mPosition.width) > MINIMUM_DIMENSION_CHANGE) ||
163 (fabs(positionSize.height - mPosition.height) > MINIMUM_DIMENSION_CHANGE) )
168 if( needToMove && needToResize)
170 ecore_x_window_move_resize(mX11Window, positionSize.x, positionSize.y, positionSize.width, positionSize.height);
171 mPosition = positionSize;
175 ecore_x_window_move(mX11Window, positionSize.x, positionSize.y);
176 mPosition = positionSize;
178 else if (needToResize)
180 ecore_x_window_resize(mX11Window, positionSize.width, positionSize.height);
181 mPosition = positionSize;
186 void WindowRenderSurface::Map()
188 ecore_x_window_show(mX11Window);
191 bool WindowRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
193 // nothing to do for windows
197 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, unsigned int, SyncMode )
199 EglImplementation& eglImpl = static_cast<EglImplementation&>( egl );
200 eglImpl.SwapBuffers();
202 // When the window is deiconified, it approves the deiconify operation to window manager after rendering
203 if(mNeedToApproveDeiconify)
205 // SwapBuffer is desychronized. So make sure to sychronize when window is deiconified.
206 glAbstraction.Finish();
208 /* client sends immediately reply message using value 1 */
209 ecore_x_client_message32_send(mX11Window,
210 ECORE_X_ATOM_E_DEICONIFY_APPROVE,
211 ECORE_X_EVENT_MASK_WINDOW_CONFIGURE,
217 mNeedToApproveDeiconify = false;
221 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
223 Ecore_X_Atom viewModeAtom( ecore_x_atom_get( "_E_COMP_3D_APP_WIN" ) );
225 if( viewModeAtom != None )
227 unsigned int value( static_cast<unsigned int>( viewMode ) );
228 ecore_x_window_prop_card32_set( mX11Window, viewModeAtom, &value, 1 );
232 void WindowRenderSurface::CreateXRenderable()
234 // if width or height are zero, go full screen.
235 if ( (mPosition.width == 0) || (mPosition.height == 0) )
237 // Default window size == screen size
241 ecore_x_screen_size_get( ecore_x_default_screen_get(), &mPosition.width, &mPosition.height );
244 if(mColorDepth == COLOR_DEPTH_32)
246 // create 32 bit window
247 mX11Window = ecore_x_window_argb_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height );
251 // create 24 bit window
252 mX11Window = ecore_x_window_new( 0, mPosition.x, mPosition.y, mPosition.width, mPosition.height );
255 if ( mX11Window == 0 )
257 DALI_ASSERT_ALWAYS(0 && "Failed to create X window");
260 // set up window title which will be helpful for debug utitilty
261 ecore_x_icccm_title_set( mX11Window, mTitle.c_str() );
262 ecore_x_netwm_name_set( mX11Window, mTitle.c_str() );
264 // set up etc properties to match with ecore-evas
266 if( ( id = getenv("DESKTOP_STARTUP_ID") ) )
268 ecore_x_netwm_startup_id_set( mX11Window, id );
271 ecore_x_icccm_hints_set( mX11Window,
273 ECORE_X_WINDOW_STATE_HINT_NORMAL, // initial_state
280 // we SHOULD guarantee the x11 window was created in x server.
284 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
286 mX11Window = static_cast< Ecore_X_Window >( surfaceId );
291 } // namespace Adaptor
293 } // namespace Internal