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 "render-thread.h"
22 #include <dali/integration-api/debug.h>
25 #include <base/interfaces/adaptor-internal-services.h>
26 #include <base/update-render-synchronization.h>
27 #include <base/environment-options.h>
28 #include <base/display-connection.h>
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gRenderLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RENDER_THREAD");
46 RenderRequest::RenderRequest(RenderRequest::Request type)
51 RenderRequest::Request RenderRequest::GetType()
56 ReplaceSurfaceRequest::ReplaceSurfaceRequest()
57 : RenderRequest(RenderRequest::REPLACE_SURFACE),
59 mReplaceCompleted(false)
63 void ReplaceSurfaceRequest::SetSurface(RenderSurface* newSurface)
65 mNewSurface = newSurface;
68 RenderSurface* ReplaceSurfaceRequest::GetSurface()
73 void ReplaceSurfaceRequest::ReplaceCompleted()
75 mReplaceCompleted = true;
78 bool ReplaceSurfaceRequest::GetReplaceCompleted()
80 return mReplaceCompleted != 0u;
84 RenderThread::RenderThread( UpdateRenderSynchronization& sync,
85 AdaptorInternalServices& adaptorInterfaces,
86 const EnvironmentOptions& environmentOptions )
87 : mUpdateRenderSync( sync ),
88 mCore( adaptorInterfaces.GetCore() ),
89 mGLES( adaptorInterfaces.GetGlesInterface() ),
90 mEglFactory( &adaptorInterfaces.GetEGLFactoryInterface()),
93 mEnvironmentOptions( environmentOptions ),
94 mSurfaceReplaced(false)
96 // set the initial values before render thread starts
97 mSurface = adaptorInterfaces.GetRenderSurfaceInterface();
99 mDisplayConnection = Dali::DisplayConnection::New();
102 RenderThread::~RenderThread()
104 if (mDisplayConnection)
106 delete mDisplayConnection;
107 mDisplayConnection = NULL;
110 DALI_ASSERT_ALWAYS( mThread == NULL && "RenderThread is still alive");
111 mEglFactory->Destroy();
114 void RenderThread::Start()
116 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Start()\n");
118 // initialise GL and kick off render thread
119 DALI_ASSERT_ALWAYS( !mEGL && "Egl already initialized" );
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" );
128 mSurface->StartRender();
132 void RenderThread::Stop()
134 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Stop()\n");
138 // Tell surface we have stopped rendering
139 mSurface->StopRender();
141 // The surface will be destroyed soon; this pointer will become invalid
145 // shutdown the render thread and destroy the opengl context
148 // wait for the thread to finish
149 pthread_join(*mThread, NULL);
156 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
157 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
158 // The following methods are all executed inside render thread !!!
159 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
160 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162 bool RenderThread::Run()
164 // install a function for logging
165 mEnvironmentOptions.InstallLogFunction();
169 bool running( true );
171 Dali::Integration::RenderStatus renderStatus;
173 uint64_t currentTime( 0 );
175 // render loop, we stay inside here when rendering
178 // Sync with update thread and get any outstanding requests from UpdateRenderSynchronization
179 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 1 - RenderSyncWithUpdate()\n");
180 RenderRequest* request = NULL;
181 running = mUpdateRenderSync.RenderSyncWithUpdate( request );
183 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 2 - Process requests\n");
185 // Consume any pending events to avoid memory leaks
186 mDisplayConnection->ConsumeEvents();
188 bool processRequests = true;
189 bool requestProcessed = false;
190 while( processRequests && running)
192 // Check if we've got any requests from the main thread (e.g. replace surface)
193 requestProcessed = ProcessRequest( request );
195 // perform any pre-render operations
196 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 3 - PreRender\n");
197 bool preRendered = PreRender(); // Returns false if no surface onto which to render
200 processRequests = false;
204 // Block until new surface... - cleared by ReplaceSurface code in UpdateRenderController
205 running = mUpdateRenderSync.RenderSyncWithRequest(request);
212 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 4 - Core.Render()\n");
213 mCore.Render( renderStatus );
215 // Notify the update-thread that a render has completed
216 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 5 - Sync.RenderFinished()\n");
217 mUpdateRenderSync.RenderFinished( renderStatus.NeedsUpdate(), requestProcessed );
219 uint64_t newTime( mUpdateRenderSync.GetTimeMicroseconds() );
221 // perform any post-render operations
222 if ( renderStatus.HasRendered() )
224 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 6 - PostRender()\n");
225 PostRender( static_cast< unsigned int >(newTime - currentTime) );
228 currentTime = newTime;
235 // install a function for logging
236 mEnvironmentOptions.UnInstallLogFunction();
241 void RenderThread::InitializeEgl()
243 mEGL = mEglFactory->Create();
245 DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
247 // initialize egl & OpenGL
248 mDisplayConnection->InitializeEgl( *mEGL );
249 mSurface->InitializeEgl( *mEGL );
251 // create the OpenGL context
252 mEGL->CreateContext();
254 // create the OpenGL surface
255 mSurface->CreateEglSurface(*mEGL);
258 mEGL->MakeContextCurrent();
260 // set the initial sync mode
262 // tell core it has a context
263 mCore.ContextCreated();
267 bool RenderThread::ProcessRequest( RenderRequest* request )
269 bool processedRequest = false;
271 if( request != NULL )
273 switch(request->GetType())
275 case RenderRequest::REPLACE_SURFACE:
277 // change the surface
278 ReplaceSurfaceRequest* replaceSurfaceRequest = static_cast<ReplaceSurfaceRequest*>(request);
279 ReplaceSurface( replaceSurfaceRequest->GetSurface() );
280 replaceSurfaceRequest->ReplaceCompleted();
281 processedRequest = true;
286 return processedRequest;
289 void RenderThread::ReplaceSurface( RenderSurface* newSurface )
291 // This is designed for replacing pixmap surfaces, but should work for window as well
292 // we need to delete the egl surface and renderable (pixmap / window)
293 // Then create a new pixmap/window and new egl surface
294 // If the new surface has a different display connection, then the context will be lost
295 DALI_ASSERT_ALWAYS(newSurface && "NULL surface");
297 mDisplayConnection->InitializeEgl(*mEGL);
299 bool contextLost = newSurface->ReplaceEGLSurface(*mEGL);
302 DALI_LOG_WARNING("Context lost\n");
303 mCore.ContextDestroyed();
304 mCore.ContextCreated();
307 // use the new surface from now on
308 mSurface = newSurface;
309 mSurfaceReplaced = true;
312 void RenderThread::ShutdownEgl()
314 // inform core of context destruction
315 mCore.ContextDestroyed();
319 // give a chance to destroy the OpenGL surface that created externally
320 mSurface->DestroyEglSurface( *mEGL );
323 // delete the GL context / egl surface
324 mEGL->TerminateGles();
327 bool RenderThread::PreRender()
329 bool success( false );
332 success = mSurface->PreRender( *mEGL, mGLES );
342 void RenderThread::PostRender( unsigned int timeDelta )
344 // Inform the gl implementation that rendering has finished before informing the surface
345 mGLES.PostRender(timeDelta);
349 // Inform the surface that rendering this frame has finished.
350 mSurface->PostRender( *mEGL, mGLES, mDisplayConnection, timeDelta, mSurfaceReplaced );
352 mSurfaceReplaced = false;
355 } // namespace Adaptor
357 } // namespace Internal