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" );
126 mSurface->StartRender();
129 void RenderThread::Stop()
131 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Stop()\n");
133 // shutdown the render thread and destroy the opengl context
136 // Tell surface we have stopped rendering
137 mSurface->StopRender();
139 // wait for the thread to finish
140 pthread_join(*mThread, NULL);
147 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
148 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
149 // The following methods are all executed inside render thread !!!
150 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153 bool RenderThread::Run()
155 // install a function for logging
156 mEnvironmentOptions.InstallLogFunction();
160 bool running( true );
162 Dali::Integration::RenderStatus renderStatus;
164 uint64_t currentTime( 0 );
166 // render loop, we stay inside here when rendering
169 // Sync with update thread and get any outstanding requests from UpdateRenderSynchronization
170 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 1 - RenderSyncWithUpdate()\n");
171 RenderRequest* request = NULL;
172 running = mUpdateRenderSync.RenderSyncWithUpdate( request );
174 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 2 - Process requests\n");
176 // Consume any pending events to avoid memory leaks
177 mDisplayConnection->ConsumeEvents();
179 bool processRequests = true;
180 bool requestProcessed = false;
181 while( processRequests && running)
183 // Check if we've got any requests from the main thread (e.g. replace surface)
184 requestProcessed = ProcessRequest( request );
186 // perform any pre-render operations
187 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 3 - PreRender\n");
188 bool preRendered = PreRender(); // Returns false if no surface onto which to render
191 processRequests = false;
195 // Block until new surface... - cleared by ReplaceSurface code in UpdateRenderController
196 running = mUpdateRenderSync.RenderSyncWithRequest(request);
203 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 4 - Core.Render()\n");
204 mCore.Render( renderStatus );
206 // Notify the update-thread that a render has completed
207 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 5 - Sync.RenderFinished()\n");
208 mUpdateRenderSync.RenderFinished( renderStatus.NeedsUpdate(), requestProcessed );
210 uint64_t newTime( mUpdateRenderSync.GetTimeMicroseconds() );
212 // perform any post-render operations
213 if ( renderStatus.HasRendered() )
215 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 6 - PostRender()\n");
216 PostRender( static_cast< unsigned int >(newTime - currentTime) );
219 currentTime = newTime;
226 // install a function for logging
227 mEnvironmentOptions.UnInstallLogFunction();
232 void RenderThread::InitializeEgl()
234 mEGL = mEglFactory->Create();
236 DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
238 // initialize egl & OpenGL
239 mDisplayConnection->InitializeEgl( *mEGL );
240 mSurface->InitializeEgl( *mEGL );
242 // create the OpenGL context
243 mEGL->CreateContext();
245 // create the OpenGL surface
246 mSurface->CreateEglSurface(*mEGL);
249 mEGL->MakeContextCurrent();
251 // set the initial sync mode
253 // tell core it has a context
254 mCore.ContextCreated();
258 bool RenderThread::ProcessRequest( RenderRequest* request )
260 bool processedRequest = false;
262 if( request != NULL )
264 switch(request->GetType())
266 case RenderRequest::REPLACE_SURFACE:
268 // change the surface
269 ReplaceSurfaceRequest* replaceSurfaceRequest = static_cast<ReplaceSurfaceRequest*>(request);
270 ReplaceSurface( replaceSurfaceRequest->GetSurface() );
271 replaceSurfaceRequest->ReplaceCompleted();
272 processedRequest = true;
277 return processedRequest;
280 void RenderThread::ReplaceSurface( RenderSurface* newSurface )
282 // This is designed for replacing pixmap surfaces, but should work for window as well
283 // we need to delete the egl surface and renderable (pixmap / window)
284 // Then create a new pixmap/window and new egl surface
285 // If the new surface has a different display connection, then the context will be lost
286 DALI_ASSERT_ALWAYS(newSurface && "NULL surface");
288 mDisplayConnection->InitializeEgl(*mEGL);
290 bool contextLost = newSurface->ReplaceEGLSurface(*mEGL);
293 DALI_LOG_WARNING("Context lost\n");
294 mCore.ContextDestroyed();
295 mCore.ContextCreated();
298 // use the new surface from now on
299 mSurface = newSurface;
300 mSurfaceReplaced = true;
304 void RenderThread::ShutdownEgl()
306 // inform core of context destruction
307 mCore.ContextDestroyed();
309 // give a chance to destroy the OpenGL surface that created externally
310 mSurface->DestroyEglSurface( *mEGL );
312 // delete the GL context / egl surface
313 mEGL->TerminateGles();
316 bool RenderThread::PreRender()
318 bool success = mSurface->PreRender( *mEGL, mGLES );
326 void RenderThread::PostRender( unsigned int timeDelta )
328 // Inform the gl implementation that rendering has finished before informing the surface
329 mGLES.PostRender(timeDelta);
331 // Inform the surface that rendering this frame has finished.
332 mSurface->PostRender( *mEGL, mGLES, mDisplayConnection, timeDelta, mSurfaceReplaced );
333 mSurfaceReplaced = false;
337 } // namespace Adaptor
339 } // namespace Internal