2 * Copyright (c) 2016 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 <pepper-dali/internal/compositor-impl.h>
22 #include <dali/integration-api/debug.h>
23 #include <wayland-server.h>
24 #include <tizen-extension-client-protocol.h>
25 #include <Ecore_Wayland.h>
39 #if defined(DEBUG_ENABLED)
40 Integration::Log::Filter* gPepperCompositorLogging = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_COMPOSITOR" );
43 static void EmbeddedCompositorGetSocket( void* data, struct tizen_embedded_compositor* embeddedCompositor, int fd )
45 int* socketFd = (int*)data;
51 static const struct tizen_embedded_compositor_listener tizenEmbeddedCompositorListener =
53 EmbeddedCompositorGetSocket
56 } // unnamed namespace
58 CompositorPtr Compositor::New( Application application, const std::string& name )
60 CompositorPtr impl = new Compositor();
62 // Second-phase init of the implementation
63 impl->Initialize( application, name );
68 Compositor::Compositor()
69 : mCompositor( NULL ),
81 Compositor::~Compositor()
85 wayland_tbm_server_deinit( mTbmServer );
90 pepper_compositor_destroy( mCompositor );
95 ecore_main_fd_handler_del( mFdHandler );
99 const std::string& Compositor::GetName() const
104 void* Compositor::GetCompositorHandle()
106 return static_cast< void* >( mCompositor );
109 void Compositor::Initialize( Application application, const std::string& name )
111 // Get socket fd from server
112 int socketFd = GetSocketFdFromServer();
115 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: socket fd is invalid.\n" );
120 mCompositor = pepper_compositor_create_fd( name.c_str(), socketFd );
123 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: pepper_compositor_create_fd is failed. [%d]\n", socketFd );
127 mDisplay = pepper_compositor_get_display( mCompositor );
130 mShell = Pepper::Shell::New( Pepper::Compositor( this ) );
133 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create shell.\n" );
134 pepper_compositor_destroy( mCompositor );
139 mTbmServer = wayland_tbm_server_init( mDisplay, NULL, -1, 0 );
142 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: wayland_tbm_server_init is failed.\n" );
143 pepper_compositor_destroy( mCompositor );
149 mInput = Pepper::Input::New( Pepper::Compositor( this ) );
152 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create input.\n" );
153 wayland_tbm_server_deinit( mTbmServer );
155 pepper_compositor_destroy( mCompositor );
160 mSocketName = pepper_compositor_get_socket_name( mCompositor );
162 mEventLoop = wl_display_get_event_loop( mDisplay );
164 int loopFd = wl_event_loop_get_fd( mEventLoop );
166 mFdHandler = ecore_main_fd_handler_add( loopFd, (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR), Compositor::FdReadCallback, this, NULL, NULL );
168 ecore_main_fd_handler_prepare_callback_set( mFdHandler, Compositor::FdPrepareCallback, this );
171 mOutput = Pepper::Output::New( Pepper::Compositor( this ), application, mInput );
174 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create output.\n" );
176 ecore_main_fd_handler_del( mFdHandler );
178 wayland_tbm_server_deinit( mTbmServer );
180 pepper_compositor_destroy( mCompositor );
185 mOutput.ObjectViewAddedSignal().Connect( this, &Compositor::OnObjectViewAdded );
186 mOutput.ObjectViewDeletedSignal().Connect( this, &Compositor::OnObjectViewDeleted );
188 DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::Initialize: success. socket name = %s\n", mSocketName.c_str() );
191 int Compositor::GetSocketFdFromServer()
195 Ecore_Wl_Global* global;
196 struct tizen_embedded_compositor* embeddedCompositor = NULL;
199 list = ecore_wl_globals_get();
202 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::GetSocketFdFromServer: ecore_wl_globals_get returns NULL.\n" );
206 EINA_INLIST_FOREACH_SAFE( list, tmp, global )
208 if( !strcmp( global->interface, "tizen_embedded_compositor" ) )
210 embeddedCompositor = static_cast< struct tizen_embedded_compositor* >( wl_registry_bind( ecore_wl_registry_get(), global->id, &tizen_embedded_compositor_interface, 1 ) );
212 tizen_embedded_compositor_add_listener( embeddedCompositor, &tizenEmbeddedCompositorListener, &fd );
217 if( !embeddedCompositor )
219 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::GetSocketFdFromServer: tizen_embedded_compositor is not supported.\n" );
223 tizen_embedded_compositor_get_socket( embeddedCompositor );
226 tizen_embedded_compositor_destroy( embeddedCompositor );
231 Pepper::Compositor::CompositorSignalType& Compositor::ObjectViewAddedSignal()
233 return mObjectViewAddedSignal;
236 Pepper::Compositor::CompositorSignalType& Compositor::ObjectViewDeletedSignal()
238 return mObjectViewDeletedSignal;
241 void Compositor::OnObjectViewAdded( Pepper::Output output, Pepper::ObjectView objectView )
243 Pepper::Compositor handle( this );
244 mObjectViewAddedSignal.Emit( handle, objectView );
246 DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::ObjectViewAddedSignal: ObjectView is added!\n" );
249 void Compositor::OnObjectViewDeleted( Pepper::Output output, Pepper::ObjectView objectView )
251 Pepper::Compositor handle( this );
252 mObjectViewDeletedSignal.Emit( handle, objectView );
254 DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::ObjectViewDeletedSignal: ObjectView is deleted!\n" );
257 Eina_Bool Compositor::FdReadCallback( void* data, Ecore_Fd_Handler* handler )
259 Compositor* compositor = static_cast< Compositor* >( data );
261 wl_event_loop_dispatch( compositor->mEventLoop, 0 );
263 return ECORE_CALLBACK_RENEW;
266 void Compositor::FdPrepareCallback( void* data, Ecore_Fd_Handler* handler )
268 Compositor* compositor = static_cast< Compositor* >( data );
270 wl_display_flush_clients( compositor->mDisplay );
273 } // namespace Internal
275 } // namespace Pepper