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 boost::thread(boost::bind(&RenderThread::Run, this));
124 mSurface->StartRender();
127 void RenderThread::Stop()
129 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Stop()\n");
131 // shutdown the render thread and destroy the opengl context
134 // Tell surface we have stopped rendering
135 mSurface->StopRender();
137 // wait for the thread to finish
145 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147 // The following methods are all executed inside render thread !!!
148 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
149 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151 bool RenderThread::Run()
153 // install a function for logging
154 mEnvironmentOptions.InstallLogFunction();
158 bool running( true );
160 Dali::Integration::RenderStatus renderStatus;
162 uint64_t currentTime( 0 );
164 // render loop, we stay inside here when rendering
167 // Sync with update thread and get any outstanding requests from UpdateRenderSynchronization
168 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 1 - RenderSyncWithUpdate()\n");
169 RenderRequest* request = NULL;
170 running = mUpdateRenderSync.RenderSyncWithUpdate( request );
172 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 2 - Process requests\n");
174 // Consume any pending events to avoid memory leaks
175 mDisplayConnection->ConsumeEvents();
177 bool processRequests = true;
178 bool requestProcessed = false;
179 while( processRequests && running)
181 // Check if we've got any requests from the main thread (e.g. replace surface)
182 requestProcessed = ProcessRequest( request );
184 // perform any pre-render operations
185 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 3 - PreRender\n");
186 bool preRendered = PreRender(); // Returns false if no surface onto which to render
189 processRequests = false;
193 // Block until new surface... - cleared by ReplaceSurface code in UpdateRenderController
194 running = mUpdateRenderSync.RenderSyncWithRequest(request);
201 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 4 - Core.Render()\n");
202 mCore.Render( renderStatus );
204 // Notify the update-thread that a render has completed
205 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 5 - Sync.RenderFinished()\n");
206 mUpdateRenderSync.RenderFinished( renderStatus.NeedsUpdate(), requestProcessed );
208 uint64_t newTime( mUpdateRenderSync.GetTimeMicroseconds() );
210 // perform any post-render operations
211 if ( renderStatus.HasRendered() )
213 DALI_LOG_INFO( gRenderLogFilter, Debug::Verbose, "RenderThread::Run. 6 - PostRender()\n");
214 PostRender( static_cast< unsigned int >(newTime - currentTime) );
217 currentTime = newTime;
224 // install a function for logging
225 mEnvironmentOptions.UnInstallLogFunction();
230 void RenderThread::InitializeEgl()
232 mEGL = mEglFactory->Create();
234 DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
236 // initialize egl & OpenGL
237 mDisplayConnection->InitializeEgl( *mEGL );
238 mSurface->InitializeEgl( *mEGL );
240 // create the OpenGL context
241 mEGL->CreateContext();
243 // create the OpenGL surface
244 mSurface->CreateEglSurface(*mEGL);
247 mEGL->MakeContextCurrent();
249 // set the initial sync mode
251 // tell core it has a context
252 mCore.ContextCreated();
256 bool RenderThread::ProcessRequest( RenderRequest* request )
258 bool processedRequest = false;
260 if( request != NULL )
262 switch(request->GetType())
264 case RenderRequest::REPLACE_SURFACE:
266 // change the surface
267 ReplaceSurfaceRequest* replaceSurfaceRequest = static_cast<ReplaceSurfaceRequest*>(request);
268 ReplaceSurface( replaceSurfaceRequest->GetSurface() );
269 replaceSurfaceRequest->ReplaceCompleted();
270 processedRequest = true;
275 return processedRequest;
278 void RenderThread::ReplaceSurface( RenderSurface* newSurface )
280 // This is designed for replacing pixmap surfaces, but should work for window as well
281 // we need to delete the egl surface and renderable (pixmap / window)
282 // Then create a new pixmap/window and new egl surface
283 // If the new surface has a different display connection, then the context will be lost
284 DALI_ASSERT_ALWAYS(newSurface && "NULL surface");
286 mDisplayConnection->InitializeEgl(*mEGL);
288 bool contextLost = newSurface->ReplaceEGLSurface(*mEGL);
291 DALI_LOG_WARNING("Context lost\n");
292 mCore.ContextDestroyed();
293 mCore.ContextCreated();
296 // use the new surface from now on
297 mSurface = newSurface;
298 mSurfaceReplaced = true;
302 void RenderThread::ShutdownEgl()
304 // inform core of context destruction
305 mCore.ContextDestroyed();
307 // give a chance to destroy the OpenGL surface that created externally
308 mSurface->DestroyEglSurface( *mEGL );
310 // delete the GL context / egl surface
311 mEGL->TerminateGles();
314 bool RenderThread::PreRender()
316 bool success = mSurface->PreRender( *mEGL, mGLES );
324 void RenderThread::PostRender( unsigned int timeDelta )
326 // Inform the gl implementation that rendering has finished before informing the surface
327 mGLES.PostRender(timeDelta);
329 // Inform the surface that rendering this frame has finished.
330 mSurface->PostRender( *mEGL, mGLES, mDisplayConnection, timeDelta, mSurfaceReplaced );
331 mSurfaceReplaced = false;
335 } // namespace Adaptor
337 } // namespace Internal