Add ObjectView::SetVisibilityState() API.
[platform/core/uifw/pepper-dali.git] / pepper-dali / internal / compositor-impl.cpp
1 /*
2  * Copyright (c) 2016 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 <pepper-dali/internal/compositor-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <pepper-dali/internal/extensions/tizen-policy.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <wayland-server.h>
27 #include <wayland-tbm-server.h>
28 #include <tizen-extension-client-protocol.h>
29 #include <Ecore_Wayland.h>
30
31 namespace Dali
32 {
33
34 namespace Pepper
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
43 #if defined(DEBUG_ENABLED)
44 Integration::Log::Filter* gPepperCompositorLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_COMPOSITOR" );
45 #endif
46
47 static void EmbeddedCompositorGetSocket( void* data, struct tizen_embedded_compositor* embeddedCompositor, int fd )
48 {
49   int* socketFd = (int*)data;
50
51   *socketFd = fd;
52   return;
53 }
54
55 static const struct tizen_embedded_compositor_listener tizenEmbeddedCompositorListener =
56 {
57   EmbeddedCompositorGetSocket
58 };
59
60 } // unnamed namespace
61
62 CompositorPtr Compositor::New( Application application, const std::string& name )
63 {
64   CompositorPtr impl = new Compositor();
65
66   // Second-phase init of the implementation
67   impl->Initialize( application, name );
68
69   return impl;
70 }
71
72 Compositor::Compositor()
73 : mCompositor( NULL ),
74   mDisplay( NULL ),
75   mEventLoop( NULL ),
76   mFdHandler( NULL ),
77   mTbmServer( NULL ),
78   mShell( NULL ),
79   mInput( NULL ),
80   mOutput( NULL ),
81   mSocketName()
82 {
83 }
84
85 Compositor::~Compositor()
86 {
87   Extension::TizenPolicyShutdown();
88
89   if( mTbmServer )
90   {
91     wayland_tbm_server_deinit( mTbmServer );
92   }
93
94   if( mCompositor )
95   {
96     pepper_compositor_destroy( mCompositor );
97   }
98
99   if( mFdHandler )
100   {
101     ecore_main_fd_handler_del( mFdHandler );
102   }
103 }
104
105 const std::string& Compositor::GetName() const
106 {
107   return mSocketName;
108 }
109
110 void* Compositor::GetCompositorHandle()
111 {
112   return static_cast< void* >( mCompositor );
113 }
114
115 void Compositor::Initialize( Application application, const std::string& name )
116 {
117   // Get socket fd from server
118   int socketFd = GetSocketFdFromServer();
119   if( socketFd == -1 )
120   {
121     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: socket fd is invalid.\n" );
122     return;
123   }
124
125   // create compositor
126   mCompositor = pepper_compositor_create_fd( name.c_str(), socketFd );
127   if( !mCompositor )
128   {
129     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: pepper_compositor_create_fd is failed. [%d]\n", socketFd );
130     return;
131   }
132
133   mDisplay = pepper_compositor_get_display( mCompositor );
134
135   // create shell
136   mShell = Pepper::Shell::New( Pepper::Compositor( this ) );
137   if( !mShell )
138   {
139     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create shell.\n" );
140     pepper_compositor_destroy( mCompositor );
141     mCompositor = NULL;
142     return;
143   }
144
145   if( !Extension::TizenPolicyInit( mCompositor ) )
146   {
147     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to init extension.\n" );
148     pepper_compositor_destroy( mCompositor );
149     mCompositor = NULL;
150     return;
151   }
152
153   mTbmServer = wayland_tbm_server_init( mDisplay, NULL, -1, 0 );
154   if( !mTbmServer )
155   {
156     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: wayland_tbm_server_init is failed.\n" );
157     Extension::TizenPolicyShutdown();
158     pepper_compositor_destroy( mCompositor );
159     mCompositor = NULL;
160     return;
161   }
162
163   // create input
164   mInput = Pepper::Input::New( Pepper::Compositor( this ) );
165   if( !mInput )
166   {
167     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create input.\n" );
168     wayland_tbm_server_deinit( mTbmServer );
169     mTbmServer = NULL;
170     Extension::TizenPolicyShutdown();
171     pepper_compositor_destroy( mCompositor );
172     mCompositor = NULL;
173     return;
174   }
175
176   mSocketName = pepper_compositor_get_socket_name( mCompositor );
177
178   mEventLoop = wl_display_get_event_loop( mDisplay );
179
180   int loopFd = wl_event_loop_get_fd( mEventLoop );
181
182   mFdHandler = ecore_main_fd_handler_add( loopFd, (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR), Compositor::FdReadCallback, this, NULL, NULL );
183
184   ecore_main_fd_handler_prepare_callback_set( mFdHandler, Compositor::FdPrepareCallback, this );
185
186   // create output
187   mOutput = Pepper::Output::New( Pepper::Compositor( this ), application, mInput );
188   if( !mOutput )
189   {
190     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create output.\n" );
191
192     ecore_main_fd_handler_del( mFdHandler );
193     mFdHandler = NULL;
194     wayland_tbm_server_deinit( mTbmServer );
195     mTbmServer = NULL;
196     Extension::TizenPolicyShutdown();
197     pepper_compositor_destroy( mCompositor );
198     mCompositor = NULL;
199     return;
200   }
201
202   mOutput.ObjectViewAddedSignal().Connect( this, &Compositor::OnObjectViewAdded );
203   mOutput.ObjectViewDeletedSignal().Connect( this, &Compositor::OnObjectViewDeleted );
204
205   DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::Initialize: success. socket name = %s\n", mSocketName.c_str() );
206 }
207
208 int Compositor::GetSocketFdFromServer()
209 {
210   Eina_Inlist* list;
211   Eina_Inlist* tmp;
212   Ecore_Wl_Global* global;
213   struct tizen_embedded_compositor* embeddedCompositor = NULL;
214   int fd = -1;
215
216   list = ecore_wl_globals_get();
217   if( !list )
218   {
219     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::GetSocketFdFromServer: ecore_wl_globals_get returns NULL.\n" );
220     return -1;
221   }
222
223   EINA_INLIST_FOREACH_SAFE( list, tmp, global )
224   {
225     if( !strcmp( global->interface, "tizen_embedded_compositor" ) )
226     {
227       embeddedCompositor = static_cast< struct tizen_embedded_compositor* >( wl_registry_bind( ecore_wl_registry_get(), global->id, &tizen_embedded_compositor_interface, 1 ) );
228
229       tizen_embedded_compositor_add_listener( embeddedCompositor, &tizenEmbeddedCompositorListener, &fd );
230       break;
231     }
232   }
233
234   if( !embeddedCompositor )
235   {
236     DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::GetSocketFdFromServer: tizen_embedded_compositor is not supported.\n" );
237     return -1;
238   }
239
240   tizen_embedded_compositor_get_socket( embeddedCompositor );
241   ecore_wl_sync();
242
243   tizen_embedded_compositor_destroy( embeddedCompositor );
244
245   return fd;
246 }
247
248 Pepper::Compositor::CompositorSignalType& Compositor::ObjectViewAddedSignal()
249 {
250   return mObjectViewAddedSignal;
251 }
252
253 Pepper::Compositor::CompositorSignalType& Compositor::ObjectViewDeletedSignal()
254 {
255   return mObjectViewDeletedSignal;
256 }
257
258 void Compositor::OnObjectViewAdded( Pepper::Output output, Pepper::ObjectView objectView )
259 {
260   Pepper::Compositor handle( this );
261   mObjectViewAddedSignal.Emit( handle, objectView );
262
263   DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::ObjectViewAddedSignal: ObjectView is added!\n" );
264 }
265
266 void Compositor::OnObjectViewDeleted( Pepper::Output output, Pepper::ObjectView objectView )
267 {
268   Pepper::Compositor handle( this );
269   mObjectViewDeletedSignal.Emit( handle, objectView );
270
271   DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::ObjectViewDeletedSignal: ObjectView is deleted!\n" );
272 }
273
274 Eina_Bool Compositor::FdReadCallback( void* data, Ecore_Fd_Handler* handler )
275 {
276   Compositor* compositor = static_cast< Compositor* >( data );
277
278   wl_event_loop_dispatch( compositor->mEventLoop, 0 );
279
280   return ECORE_CALLBACK_RENEW;
281 }
282
283 void Compositor::FdPrepareCallback( void* data, Ecore_Fd_Handler* handler )
284 {
285   Compositor* compositor = static_cast< Compositor* >( data );
286
287   wl_display_flush_clients( compositor->mDisplay );
288 }
289
290 } // namespace Internal
291
292 } // namespace Pepper
293
294 } // namespace Dali