Added network build option. Disabled by default.
[platform/core/uifw/dali-adaptor.git] / adaptors / base / render-thread.cpp
1 /*
2  * Copyright (c) 2015 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 "render-thread.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <base/interfaces/adaptor-internal-services.h>
26 #include <base/thread-synchronization.h>
27 #include <base/environment-options.h>
28 #include <base/display-connection.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace Adaptor
37 {
38
39 namespace
40 {
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gRenderLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RENDER_THREAD");
43 #endif
44 }
45
46 RenderRequest::RenderRequest(RenderRequest::Request type)
47 : mRequestType(type)
48 {
49 }
50
51 RenderRequest::Request RenderRequest::GetType()
52 {
53   return mRequestType;
54 }
55
56 ReplaceSurfaceRequest::ReplaceSurfaceRequest()
57 : RenderRequest(RenderRequest::REPLACE_SURFACE),
58   mNewSurface( NULL ),
59   mReplaceCompleted(false)
60 {
61 }
62
63 void ReplaceSurfaceRequest::SetSurface(RenderSurface* newSurface)
64 {
65   mNewSurface = newSurface;
66 }
67
68 RenderSurface* ReplaceSurfaceRequest::GetSurface()
69 {
70   return mNewSurface;
71 }
72
73 void ReplaceSurfaceRequest::ReplaceCompleted()
74 {
75   mReplaceCompleted = true;
76 }
77
78 bool ReplaceSurfaceRequest::GetReplaceCompleted()
79 {
80   return mReplaceCompleted != 0u;
81 }
82
83
84 RenderThread::RenderThread( ThreadSynchronization& sync,
85                             AdaptorInternalServices& adaptorInterfaces,
86                             const EnvironmentOptions& environmentOptions )
87 : mThreadSynchronization( sync ),
88   mCore( adaptorInterfaces.GetCore() ),
89   mGLES( adaptorInterfaces.GetGlesInterface() ),
90   mEglFactory( &adaptorInterfaces.GetEGLFactoryInterface()),
91   mEGL( NULL ),
92   mThread( NULL ),
93   mEnvironmentOptions( environmentOptions ),
94   mSurfaceReplaced(false)
95 {
96   // set the initial values before render thread starts
97   mSurface = adaptorInterfaces.GetRenderSurfaceInterface();
98
99   mDisplayConnection = Dali::DisplayConnection::New();
100 }
101
102 RenderThread::~RenderThread()
103 {
104   if (mDisplayConnection)
105   {
106     delete mDisplayConnection;
107     mDisplayConnection = NULL;
108   }
109
110   DALI_ASSERT_ALWAYS( mThread == NULL && "RenderThread is still alive");
111   mEglFactory->Destroy();
112 }
113
114 void RenderThread::Start()
115 {
116   DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Start()\n");
117
118   // initialise GL and kick off render thread
119   DALI_ASSERT_ALWAYS( !mEGL && "Egl already initialized" );
120
121   // create the render thread, initially we are rendering
122   mThread = new pthread_t();
123   int error = pthread_create( mThread, NULL, InternalThreadEntryFunc, this );
124   DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() in RenderThread" );
125
126   if( mSurface )
127   {
128     mSurface->StartRender();
129   }
130 }
131
132 void RenderThread::Stop()
133 {
134   DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Stop()\n");
135
136   if( mSurface )
137   {
138     // Tell surface we have stopped rendering
139     mSurface->StopRender();
140
141     // The surface will be destroyed soon; this pointer will become invalid
142     mSurface = NULL;
143   }
144
145   // shutdown the render thread and destroy the opengl context
146   if( mThread )
147   {
148     // wait for the thread to finish
149     pthread_join(*mThread, NULL);
150
151     delete mThread;
152     mThread = NULL;
153   }
154 }
155
156 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
157 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
158 // The following methods are all executed inside render thread !!!
159 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
160 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
161
162 bool RenderThread::Run()
163 {
164   DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run\n");
165
166   // Install a function for logging
167   mEnvironmentOptions.InstallLogFunction();
168
169   InitializeEgl();
170
171   Dali::Integration::RenderStatus renderStatus;
172   RenderRequest* request = NULL;
173
174   // Render loop, we stay inside here when rendering
175   while( mThreadSynchronization.RenderReady( request ) )
176   {
177     DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 1 - RenderReady\n");
178
179     // Consume any pending events to avoid memory leaks
180     DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 2 - ConsumeEvents\n");
181     mDisplayConnection->ConsumeEvents();
182
183     // Check if we've got a request from the main thread (e.g. replace surface)
184     if( request )
185     {
186       // Process the request, we should NOT render when we have a request
187       DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 3 - Process requests\n");
188       ProcessRequest( request );
189     }
190     else
191     {
192       // No request to process so we render
193       if( PreRender() ) // Returns false if no surface onto which to render
194       {
195         // Render
196         DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 3 - Core.Render()\n");
197
198         mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::RENDER_START );
199         mCore.Render( renderStatus );
200         mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::RENDER_END );
201
202         // Perform any post-render operations
203         if ( renderStatus.HasRendered() )
204         {
205           DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 4 - PostRender()\n");
206           PostRender();
207         }
208       }
209     }
210
211     request = NULL; // Clear the request if it was set, no need to release memory
212   }
213
214   // Shut down EGL
215   ShutdownEgl();
216
217   // Uninstall the logging function
218   mEnvironmentOptions.UnInstallLogFunction();
219
220   return true;
221 }
222
223 void RenderThread::InitializeEgl()
224 {
225   mEGL = mEglFactory->Create();
226
227   DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
228
229   // initialize egl & OpenGL
230   mDisplayConnection->InitializeEgl( *mEGL );
231   mSurface->InitializeEgl( *mEGL );
232
233   // create the OpenGL context
234   mEGL->CreateContext();
235
236   // create the OpenGL surface
237   mSurface->CreateEglSurface(*mEGL);
238
239   // Make it current
240   mEGL->MakeContextCurrent();
241
242   // set the initial sync mode
243
244   // tell core it has a context
245   mCore.ContextCreated();
246
247 }
248
249 void RenderThread::ProcessRequest( RenderRequest* request )
250 {
251   if( request != NULL )
252   {
253     switch(request->GetType())
254     {
255       case RenderRequest::REPLACE_SURFACE:
256       {
257         // change the surface
258         ReplaceSurfaceRequest* replaceSurfaceRequest = static_cast<ReplaceSurfaceRequest*>(request);
259         ReplaceSurface( replaceSurfaceRequest->GetSurface() );
260         replaceSurfaceRequest->ReplaceCompleted();
261         mThreadSynchronization.RenderInformSurfaceReplaced();
262         break;
263       }
264     }
265   }
266 }
267
268 void RenderThread::ReplaceSurface( RenderSurface* newSurface )
269 {
270   // This is designed for replacing pixmap surfaces, but should work for window as well
271   // we need to delete the egl surface and renderable (pixmap / window)
272   // Then create a new pixmap/window and new egl surface
273   // If the new surface has a different display connection, then the context will be lost
274   DALI_ASSERT_ALWAYS(newSurface && "NULL surface");
275
276   mDisplayConnection->InitializeEgl(*mEGL);
277
278   newSurface->ReplaceEGLSurface(*mEGL);
279
280   // use the new surface from now on
281   mSurface = newSurface;
282   mSurfaceReplaced = true;
283 }
284
285 void RenderThread::ShutdownEgl()
286 {
287   // inform core of context destruction
288   mCore.ContextDestroyed();
289
290   if( mSurface )
291   {
292     // give a chance to destroy the OpenGL surface that created externally
293     mSurface->DestroyEglSurface( *mEGL );
294   }
295
296   // delete the GL context / egl surface
297   mEGL->TerminateGles();
298 }
299
300 bool RenderThread::PreRender()
301 {
302   bool success( false );
303   if( mSurface )
304   {
305     success = mSurface->PreRender( *mEGL, mGLES );
306   }
307
308   if( success )
309   {
310     mGLES.PreRender();
311   }
312   return success;
313 }
314
315 void RenderThread::PostRender()
316 {
317   // Inform the gl implementation that rendering has finished before informing the surface
318   mGLES.PostRender();
319
320   if( mSurface )
321   {
322     // Inform the surface that rendering this frame has finished.
323     mSurface->PostRender( *mEGL, mGLES, mDisplayConnection, mSurfaceReplaced );
324   }
325   mSurfaceReplaced = false;
326 }
327
328 } // namespace Adaptor
329
330 } // namespace Internal
331
332 } // namespace Dali