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"
23 #include <dali/integration-api/debug.h>
24 #include <base/interfaces/adaptor-internal-services.h>
25 #include <base/update-render-synchronization.h>
26 #include <base/environment-options.h>
40 #if defined(DEBUG_ENABLED)
41 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();
100 RenderThread::~RenderThread()
102 DALI_ASSERT_ALWAYS( mThread == NULL && "RenderThread is still alive");
103 mEglFactory->Destroy();
106 void RenderThread::Start()
108 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Start()\n");
110 // initialise GL and kick off render thread
111 DALI_ASSERT_ALWAYS( !mEGL && "Egl already initialized" );
113 // create the render thread, initially we are rendering
114 mThread = new boost::thread(boost::bind(&RenderThread::Run, this));
116 mSurface->StartRender();
119 void RenderThread::Stop()
121 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Stop()\n");
123 // shutdown the render thread and destroy the opengl context
126 // Tell surface we have stopped rendering
127 mSurface->StopRender();
129 // wait for the thread to finish
137 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139 // The following methods are all executed inside render thread !!!
140 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
143 bool RenderThread::Run()
145 // install a function for logging
146 mEnvironmentOptions.InstallLogFunction();
150 bool running( true );
152 Dali::Integration::RenderStatus renderStatus;
154 uint64_t currentTime( 0 );
156 // render loop, we stay inside here when rendering
159 // Sync with update thread and get any outstanding requests from UpdateRenderSynchronization
160 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 1 - RenderSyncWithUpdate()\n");
161 RenderRequest* request = NULL;
162 running = mUpdateRenderSync.RenderSyncWithUpdate( request );
164 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 2 - Process requests\n");
166 // Consume any pending events
169 bool processRequests = true;
170 bool requestProcessed = false;
171 while( processRequests && running)
173 // Check if we've got any requests from the main thread (e.g. replace surface)
174 requestProcessed = ProcessRequest( request );
176 // perform any pre-render operations
177 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 3 - PreRender\n");
178 bool preRendered = PreRender(); // Returns false if no surface onto which to render
181 processRequests = false;
185 // Block until new surface... - cleared by ReplaceSurface code in UpdateRenderController
186 running = mUpdateRenderSync.RenderSyncWithRequest(request);
193 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 4 - Core.Render()\n");
194 mCore.Render( renderStatus );
196 // Notify the update-thread that a render has completed
197 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 5 - Sync.RenderFinished()\n");
198 mUpdateRenderSync.RenderFinished( renderStatus.NeedsUpdate(), requestProcessed );
200 uint64_t newTime( mUpdateRenderSync.GetTimeMicroseconds() );
202 // perform any post-render operations
203 if ( renderStatus.HasRendered() )
205 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 6 - PostRender()\n");
206 PostRender( static_cast< unsigned int >(newTime - currentTime) );
209 currentTime = newTime;
216 // install a function for logging
217 mEnvironmentOptions.UnInstallLogFunction();
222 void RenderThread::InitializeEgl()
224 mEGL = mEglFactory->Create();
226 DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
228 // initialize egl & OpenGL
229 mSurface->InitializeEgl( *mEGL );
231 // create the OpenGL context
232 mEGL->CreateContext();
234 // create the OpenGL surface
235 mSurface->CreateEglSurface( *mEGL );
238 mEGL->MakeContextCurrent();
240 // set the initial sync mode
243 // tell core it has a context
244 mCore.ContextCreated();
248 void RenderThread::ConsumeEvents()
250 // tell surface to consume any events to avoid memory leaks
251 mSurface->ConsumeEvents();
254 bool RenderThread::ProcessRequest( RenderRequest* request )
256 bool processedRequest = false;
258 if( request != NULL )
260 switch(request->GetType())
262 case RenderRequest::REPLACE_SURFACE:
264 // change the surface
265 ReplaceSurfaceRequest* replaceSurfaceRequest = static_cast<ReplaceSurfaceRequest*>(request);
266 ReplaceSurface( replaceSurfaceRequest->GetSurface() );
267 replaceSurfaceRequest->ReplaceCompleted();
268 processedRequest = true;
273 return processedRequest;
276 void RenderThread::ReplaceSurface( RenderSurface* newSurface )
278 // This is designed for replacing pixmap surfaces, but should work for window as well
279 // we need to delete the egl surface and renderable (pixmap / window)
280 // Then create a new pixmap/window and new egl surface
281 // If the new surface has a different display connection, then the context will be lost
282 DALI_ASSERT_ALWAYS( newSurface && "NULL surface" );
284 bool contextLost = newSurface->ReplaceEGLSurface( *mEGL );
288 DALI_LOG_WARNING("Context lost\n");
289 mCore.ContextDestroyed();
290 mCore.ContextCreated();
293 // if both new and old surface are using the same display, and the display
294 // connection was created by Dali, then transfer
295 // display owner ship to the new surface.
296 mSurface->TransferDisplayOwner( *newSurface );
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, timeDelta, mSurfaceReplaced );
333 mSurfaceReplaced = false;
337 } // namespace Adaptor
339 } // namespace Internal