2 * Copyright (c) 2019 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 <dali/internal/adaptor/common/combined-update-render-controller.h>
23 #include <dali/integration-api/platform-abstraction.h>
26 #include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
27 #include <dali/devel-api/adaptor-framework/thread-settings.h>
28 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
29 #include <dali/internal/adaptor/common/combined-update-render-controller-debug.h>
30 #include <dali/internal/graphics/gles/egl-graphics.h>
31 #include <dali/internal/graphics/gles/egl-implementation.h>
32 #include <dali/internal/graphics/common/graphics-interface.h>
33 #include <dali/internal/system/common/environment-options.h>
34 #include <dali/internal/system/common/time-service.h>
48 const unsigned int CREATED_THREAD_COUNT = 1u;
50 const int CONTINUOUS = -1;
53 const unsigned int TRUE = 1u;
54 const unsigned int FALSE = 0u;
56 const unsigned int MILLISECONDS_PER_SECOND( 1e+3 );
57 const float NANOSECONDS_TO_SECOND( 1e-9f );
58 const unsigned int NANOSECONDS_PER_SECOND( 1e+9 );
59 const unsigned int NANOSECONDS_PER_MILLISECOND( 1e+6 );
61 // The following values will get calculated at compile time
62 const float DEFAULT_FRAME_DURATION_IN_SECONDS( 1.0f / 60.0f );
63 const uint64_t DEFAULT_FRAME_DURATION_IN_MILLISECONDS( DEFAULT_FRAME_DURATION_IN_SECONDS * MILLISECONDS_PER_SECOND );
64 const uint64_t DEFAULT_FRAME_DURATION_IN_NANOSECONDS( DEFAULT_FRAME_DURATION_IN_SECONDS * NANOSECONDS_PER_SECOND );
67 * Handles the use case when an update-request is received JUST before we process a sleep-request. If we did not have an update-request count then
68 * there is a danger that, on the event-thread we could have:
69 * 1) An update-request where we do nothing as Update/Render thread still running.
70 * 2) Quickly followed by a sleep-request being handled where we pause the Update/Render Thread (even though we have an update to process).
72 * Using a counter means we increment the counter on an update-request, and decrement it on a sleep-request. This handles the above scenario because:
73 * 1) MAIN THREAD: Update Request: COUNTER = 1
74 * 2) UPDATE/RENDER THREAD: Do Update/Render, then no Updates required -> Sleep Trigger
75 * 3) MAIN THREAD: Update Request: COUNTER = 2
76 * 4) MAIN THREAD: Sleep Request: COUNTER = 1 -> We do not sleep just yet
78 * Also ensures we preserve battery life by only doing ONE update when the above use case is not triggered.
79 * 1) MAIN THREAD: Update Request: COUNTER = 1
80 * 2) UPDATE/RENDER THREAD: Do Update/Render, then no Updates required -> Sleep Trigger
81 * 3) MAIN THREAD: Sleep Request: COUNTER = 0 -> Go to sleep
83 const unsigned int MAXIMUM_UPDATE_REQUESTS = 2;
84 } // unnamed namespace
86 ///////////////////////////////////////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////////////////////////////////////
90 CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions )
91 : mFpsTracker( environmentOptions ),
92 mUpdateStatusLogger( environmentOptions ),
93 mEventThreadSemaphore(),
94 mUpdateRenderThreadWaitCondition(),
95 mAdaptorInterfaces( adaptorInterfaces ),
96 mPerformanceInterface( adaptorInterfaces.GetPerformanceInterface() ),
97 mCore( adaptorInterfaces.GetCore() ),
98 mEnvironmentOptions( environmentOptions ),
99 mNotificationTrigger( adaptorInterfaces.GetProcessCoreEventsTrigger() ),
100 mSleepTrigger( NULL ),
101 mPreRenderCallback( NULL ),
102 mUpdateRenderThread( NULL ),
103 mDefaultFrameDelta( 0.0f ),
104 mDefaultFrameDurationMilliseconds( 0u ),
105 mDefaultFrameDurationNanoseconds( 0u ),
106 mDefaultHalfFrameNanoseconds( 0u ),
107 mUpdateRequestCount( 0u ),
109 mUpdateRenderRunCount( 0 ),
110 mDestroyUpdateRenderThread( FALSE ),
111 mUpdateRenderThreadCanSleep( FALSE ),
112 mPendingRequestUpdate( FALSE ),
113 mUseElapsedTimeAfterWait( FALSE ),
115 mDeletedSurface( nullptr ),
116 mPostRendering( FALSE ),
117 mSurfaceResized( FALSE ),
118 mForceClear( FALSE ),
119 mUploadWithoutRendering( FALSE ),
120 mFirstFrameAfterResume( FALSE )
124 // Initialise frame delta/duration variables first
125 SetRenderRefreshRate( environmentOptions.GetRenderRefreshRate() );
127 // Set the thread-synchronization interface on the render-surface
128 Dali::RenderSurfaceInterface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
131 currentSurface->SetThreadSynchronization( *this );
134 TriggerEventFactoryInterface& triggerFactory = mAdaptorInterfaces.GetTriggerEventFactoryInterface();
135 mSleepTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &CombinedUpdateRenderController::ProcessSleepRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
137 sem_init( &mEventThreadSemaphore, 0, 0 ); // Initialize to 0 so that it just waits if sem_post has not been called
140 CombinedUpdateRenderController::~CombinedUpdateRenderController()
146 delete mPreRenderCallback;
147 delete mSleepTrigger;
150 void CombinedUpdateRenderController::Initialize()
154 // Ensure Update/Render Thread not already created
155 DALI_ASSERT_ALWAYS( ! mUpdateRenderThread );
157 // Create Update/Render Thread
158 mUpdateRenderThread = new pthread_t();
159 int error = pthread_create( mUpdateRenderThread, NULL, InternalUpdateRenderThreadEntryFunc, this );
160 DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() when creating UpdateRenderThread" );
162 // The Update/Render thread will now run and initialise the graphics interface etc. and will then wait for Start to be called
163 // When this function returns, the application initialisation on the event thread should occur
166 void CombinedUpdateRenderController::Start()
170 DALI_ASSERT_ALWAYS( !mRunning && mUpdateRenderThread );
172 // Wait until all threads created in Initialise are up and running
173 for( unsigned int i = 0; i < CREATED_THREAD_COUNT; ++i )
175 sem_wait( &mEventThreadSemaphore );
178 Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
181 currentSurface->StartRender();
186 LOG_EVENT( "Startup Complete, starting Update/Render Thread" );
188 RunUpdateRenderThread( CONTINUOUS, AnimationProgression::NONE, UpdateMode::NORMAL );
190 DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Start\n" );
193 void CombinedUpdateRenderController::Pause()
199 PauseUpdateRenderThread();
201 AddPerformanceMarker( PerformanceInterface::PAUSED );
203 DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Pause\n" );
206 void CombinedUpdateRenderController::Resume()
210 if( !mRunning && IsUpdateRenderThreadPaused() )
212 LOG_EVENT( "Resuming" );
214 RunUpdateRenderThread( CONTINUOUS, AnimationProgression::USE_ELAPSED_TIME, UpdateMode::NORMAL );
216 AddPerformanceMarker( PerformanceInterface::RESUME );
220 mFirstFrameAfterResume = TRUE;
222 DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Resume\n" );
226 DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Resume: Already resumed [%d, %d, %d]\n", mRunning, mUpdateRenderRunCount, mUpdateRenderThreadCanSleep );
230 void CombinedUpdateRenderController::Stop()
234 // Stop Rendering and the Update/Render Thread
235 Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
238 currentSurface->StopRender();
241 StopUpdateRenderThread();
243 if( mUpdateRenderThread )
245 LOG_EVENT( "Destroying UpdateRenderThread" );
247 // wait for the thread to finish
248 pthread_join( *mUpdateRenderThread, NULL );
250 delete mUpdateRenderThread;
251 mUpdateRenderThread = NULL;
256 DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Stop\n" );
259 void CombinedUpdateRenderController::RequestUpdate()
263 // Increment the update-request count to the maximum
264 if( mUpdateRequestCount < MAXIMUM_UPDATE_REQUESTS )
266 ++mUpdateRequestCount;
269 if( mRunning && IsUpdateRenderThreadPaused() )
271 LOG_EVENT( "Processing" );
273 RunUpdateRenderThread( CONTINUOUS, AnimationProgression::NONE, UpdateMode::NORMAL );
276 ConditionalWait::ScopedLock updateLock( mUpdateRenderThreadWaitCondition );
277 mPendingRequestUpdate = TRUE;
280 void CombinedUpdateRenderController::RequestUpdateOnce( UpdateMode updateMode )
282 // Increment the update-request count to the maximum
283 if( mUpdateRequestCount < MAXIMUM_UPDATE_REQUESTS )
285 ++mUpdateRequestCount;
288 if( IsUpdateRenderThreadPaused() )
292 // Run Update/Render once
293 RunUpdateRenderThread( ONCE, AnimationProgression::NONE, updateMode );
297 void CombinedUpdateRenderController::ReplaceSurface( Dali::RenderSurfaceInterface* newSurface )
301 if( mUpdateRenderThread )
303 // Set the ThreadSyncronizationInterface on the new surface
304 newSurface->SetThreadSynchronization( *this );
306 LOG_EVENT( "Starting to replace the surface, event-thread blocked" );
308 // Start replacing the surface.
310 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
311 mPostRendering = FALSE; // Clear the post-rendering flag as Update/Render thread will replace the surface now
312 mNewSurface = newSurface;
313 mUpdateRenderThreadWaitCondition.Notify( lock );
316 // Wait until the surface has been replaced
317 sem_wait( &mEventThreadSemaphore );
319 LOG_EVENT( "Surface replaced, event-thread continuing" );
323 void CombinedUpdateRenderController::DeleteSurface( Dali::RenderSurfaceInterface* surface )
327 if( mUpdateRenderThread )
329 LOG_EVENT( "Starting to delete the surface, event-thread blocked" );
331 // Start replacing the surface.
333 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
334 mPostRendering = FALSE; // Clear the post-rendering flag as Update/Render thread will delete the surface now
335 mDeletedSurface = surface;
336 mUpdateRenderThreadWaitCondition.Notify( lock );
339 // Wait until the surface has been deleted
340 sem_wait( &mEventThreadSemaphore );
342 LOG_EVENT( "Surface deleted, event-thread continuing" );
346 void CombinedUpdateRenderController::ResizeSurface()
350 LOG_EVENT( "Resize the surface" );
353 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
354 mPostRendering = FALSE; // Clear the post-rendering flag as Update/Render thread will resize the surface now
355 mSurfaceResized = TRUE;
356 mUpdateRenderThreadWaitCondition.Notify( lock );
360 void CombinedUpdateRenderController::SetRenderRefreshRate( unsigned int numberOfFramesPerRender )
362 // Not protected by lock, but written to rarely so not worth adding a lock when reading
363 mDefaultFrameDelta = numberOfFramesPerRender * DEFAULT_FRAME_DURATION_IN_SECONDS;
364 mDefaultFrameDurationMilliseconds = uint64_t( numberOfFramesPerRender ) * DEFAULT_FRAME_DURATION_IN_MILLISECONDS;
365 mDefaultFrameDurationNanoseconds = uint64_t( numberOfFramesPerRender ) * DEFAULT_FRAME_DURATION_IN_NANOSECONDS;
366 mDefaultHalfFrameNanoseconds = mDefaultFrameDurationNanoseconds / 2u;
368 LOG_EVENT( "mDefaultFrameDelta(%.6f), mDefaultFrameDurationMilliseconds(%lld), mDefaultFrameDurationNanoseconds(%lld)", mDefaultFrameDelta, mDefaultFrameDurationMilliseconds, mDefaultFrameDurationNanoseconds );
371 void CombinedUpdateRenderController::SetPreRenderCallback( CallbackBase* callback )
374 LOG_EVENT( "Set PreRender Callback" );
376 ConditionalWait::ScopedLock updateLock( mUpdateRenderThreadWaitCondition );
377 if( mPreRenderCallback )
379 delete mPreRenderCallback;
381 mPreRenderCallback = callback;
384 ///////////////////////////////////////////////////////////////////////////////////////////////////
386 ///////////////////////////////////////////////////////////////////////////////////////////////////
388 void CombinedUpdateRenderController::RunUpdateRenderThread( int numberOfCycles, AnimationProgression animationProgression, UpdateMode updateMode )
390 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
391 mUpdateRenderRunCount = numberOfCycles;
392 mUpdateRenderThreadCanSleep = FALSE;
393 mUseElapsedTimeAfterWait = ( animationProgression == AnimationProgression::USE_ELAPSED_TIME );
394 mUploadWithoutRendering = ( updateMode == UpdateMode::SKIP_RENDER );
395 LOG_COUNTER_EVENT( "mUpdateRenderRunCount: %d, mUseElapsedTimeAfterWait: %d", mUpdateRenderRunCount, mUseElapsedTimeAfterWait );
396 mUpdateRenderThreadWaitCondition.Notify( lock );
399 void CombinedUpdateRenderController::PauseUpdateRenderThread()
401 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
402 mUpdateRenderRunCount = 0;
405 void CombinedUpdateRenderController::StopUpdateRenderThread()
407 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
408 mDestroyUpdateRenderThread = TRUE;
409 mUpdateRenderThreadWaitCondition.Notify( lock );
412 bool CombinedUpdateRenderController::IsUpdateRenderThreadPaused()
414 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
415 return ( mUpdateRenderRunCount != CONTINUOUS ) || // Report paused if NOT continuously running
416 mUpdateRenderThreadCanSleep; // Report paused if sleeping
419 void CombinedUpdateRenderController::ProcessSleepRequest()
423 // Decrement Update request count
424 if( mUpdateRequestCount > 0 )
426 --mUpdateRequestCount;
429 // Can sleep if our update-request count is 0
430 // Update/Render thread can choose to carry on updating if it determines more update/renders are required
431 if( mUpdateRequestCount == 0 )
433 LOG_EVENT( "Going to sleep" );
435 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
436 mUpdateRenderThreadCanSleep = TRUE;
440 ///////////////////////////////////////////////////////////////////////////////////////////////////
441 // UPDATE/RENDER THREAD
442 ///////////////////////////////////////////////////////////////////////////////////////////////////
444 void CombinedUpdateRenderController::UpdateRenderThread()
446 SetThreadName("RenderThread\0");
448 // Install a function for logging
449 mEnvironmentOptions.InstallLogFunction();
451 // Install a function for tracing
452 mEnvironmentOptions.InstallTraceFunction();
454 LOG_UPDATE_RENDER( "THREAD CREATED" );
456 // Initialize EGL & OpenGL
457 Dali::DisplayConnection& displayConnection = mAdaptorInterfaces.GetDisplayConnectionInterface();
458 displayConnection.Initialize();
460 RenderSurfaceInterface* currentSurface = nullptr;
462 GraphicsInterface& graphics = mAdaptorInterfaces.GetGraphicsInterface();
463 EglGraphics* eglGraphics = static_cast<EglGraphics *>(&graphics);
465 // This will only be created once
466 EglInterface* eglInterface = &eglGraphics->GetEglInterface();
468 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( *eglInterface );
470 // Try to use OpenGL es 3.0
471 // ChooseConfig returns false here when the device only support gles 2.0.
472 // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
473 if( !eglImpl.ChooseConfig( true, COLOR_DEPTH_32 ) )
475 // Retry to use OpenGL es 2.0
476 eglGraphics->SetGlesVersion( 20 );
477 eglImpl.ChooseConfig( true, COLOR_DEPTH_32 );
480 // Check whether surfaceless context is supported
481 bool isSurfacelessContextSupported = eglImpl.IsSurfacelessContextSupported();
482 eglGraphics->SetIsSurfacelessContextSupported( isSurfacelessContextSupported );
484 if ( isSurfacelessContextSupported )
486 // Create a surfaceless OpenGL context for shared resources
487 eglImpl.CreateContext();
488 eglImpl.MakeContextCurrent( EGL_NO_SURFACE, eglImpl.GetContext() );
492 currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
495 currentSurface->InitializeGraphics();
496 currentSurface->MakeContextCurrent();
500 eglGraphics->GetGlesInterface().ContextCreated();
502 // Tell core it has a context
503 mCore.ContextCreated();
505 NotifyThreadInitialised();
508 uint64_t lastFrameTime;
509 TimeService::GetNanoseconds( lastFrameTime );
511 LOG_UPDATE_RENDER( "THREAD INITIALISED" );
513 bool useElapsedTime = true;
514 bool updateRequired = true;
515 uint64_t timeToSleepUntil = 0;
516 int extraFramesDropped = 0;
518 const unsigned int renderToFboInterval = mEnvironmentOptions.GetRenderToFboInterval();
519 const bool renderToFboEnabled = 0u != renderToFboInterval;
520 unsigned int frameCount = 0u;
522 while( UpdateRenderReady( useElapsedTime, updateRequired, timeToSleepUntil ) )
524 LOG_UPDATE_RENDER_TRACE;
526 // Performance statistics are logged upon a VSYNC tick so use this point for a VSync marker
527 AddPerformanceMarker( PerformanceInterface::VSYNC );
529 uint64_t currentFrameStartTime = 0;
530 TimeService::GetNanoseconds( currentFrameStartTime );
532 const uint64_t timeSinceLastFrame = currentFrameStartTime - lastFrameTime;
534 // Optional FPS Tracking when continuously rendering
535 if( useElapsedTime && mFpsTracker.Enabled() )
537 float absoluteTimeSinceLastRender = timeSinceLastFrame * NANOSECONDS_TO_SECOND;
538 mFpsTracker.Track( absoluteTimeSinceLastRender );
541 lastFrameTime = currentFrameStartTime; // Store frame start time
543 //////////////////////////////
545 //////////////////////////////
547 Integration::RenderSurface* newSurface = ShouldSurfaceBeReplaced();
548 if( DALI_UNLIKELY( newSurface ) )
550 LOG_UPDATE_RENDER_TRACE_FMT( "Replacing Surface" );
551 // This is designed for replacing pixmap surfaces, but should work for window as well
552 // we need to delete the surface and renderable (pixmap / window)
553 // Then create a new pixmap/window and new surface
554 // If the new surface has a different display connection, then the context will be lost
555 mAdaptorInterfaces.GetDisplayConnectionInterface().Initialize();
556 newSurface->InitializeGraphics();
557 newSurface->MakeContextCurrent();
558 // TODO: ReplaceGraphicsSurface doesn't work, InitializeGraphics()
559 // already creates new surface window, the surface and the context.
560 // We probably don't need ReplaceGraphicsSurface at all.
561 // newSurface->ReplaceGraphicsSurface();
565 const bool isRenderingToFbo = renderToFboEnabled && ( ( 0u == frameCount ) || ( 0u != frameCount % renderToFboInterval ) );
568 //////////////////////////////
570 //////////////////////////////
572 const unsigned int currentTime = currentFrameStartTime / NANOSECONDS_PER_MILLISECOND;
573 const unsigned int nextFrameTime = currentTime + mDefaultFrameDurationMilliseconds;
575 uint64_t noOfFramesSinceLastUpdate = 1;
576 float frameDelta = 0.0f;
579 // If using the elapsed time, then calculate frameDelta as a multiple of mDefaultFrameDelta
580 noOfFramesSinceLastUpdate += extraFramesDropped;
582 frameDelta = mDefaultFrameDelta * noOfFramesSinceLastUpdate;
584 LOG_UPDATE_RENDER( "timeSinceLastFrame(%llu) noOfFramesSinceLastUpdate(%u) frameDelta(%.6f)", timeSinceLastFrame, noOfFramesSinceLastUpdate, frameDelta );
586 Integration::UpdateStatus updateStatus;
588 AddPerformanceMarker( PerformanceInterface::UPDATE_START );
589 mCore.Update( frameDelta,
595 AddPerformanceMarker( PerformanceInterface::UPDATE_END );
597 unsigned int keepUpdatingStatus = updateStatus.KeepUpdating();
599 // Tell the event-thread to wake up (if asleep) and send a notification event to Core if required
600 if( updateStatus.NeedsNotification() )
602 mNotificationTrigger.Trigger();
603 LOG_UPDATE_RENDER( "Notification Triggered" );
607 bool shouldSurfaceBeResized = ShouldSurfaceBeResized();
608 if( DALI_UNLIKELY( shouldSurfaceBeResized ) )
610 if( updateStatus.SurfaceRectChanged() )
612 LOG_UPDATE_RENDER_TRACE_FMT( "Resizing Surface" );
617 // Optional logging of update/render status
618 mUpdateStatusLogger.Log( keepUpdatingStatus );
620 //////////////////////////////
622 //////////////////////////////
624 mAdaptorInterfaces.GetDisplayConnectionInterface().ConsumeEvents();
626 if( mPreRenderCallback != NULL )
628 bool keepCallback = CallbackBase::ExecuteReturn<bool>(*mPreRenderCallback);
631 delete mPreRenderCallback;
632 mPreRenderCallback = NULL;
636 if( eglImpl.IsSurfacelessContextSupported() )
638 // Make the shared surfaceless context as current before rendering
639 eglImpl.MakeContextCurrent( EGL_NO_SURFACE, eglImpl.GetContext() );
642 if( mFirstFrameAfterResume )
644 // mFirstFrameAfterResume is set to true when the thread is resumed
645 // Let eglImplementation know the first frame after thread initialized or resumed.
646 eglImpl.SetFirstFrameAfterResume();
647 mFirstFrameAfterResume = FALSE;
650 Integration::RenderStatus renderStatus;
652 AddPerformanceMarker( PerformanceInterface::RENDER_START );
653 mCore.Render( renderStatus, mForceClear, mUploadWithoutRendering );
655 //////////////////////////////
657 //////////////////////////////
659 Integration::RenderSurface* deletedSurface = ShouldSurfaceBeDeleted();
660 if( DALI_UNLIKELY( deletedSurface ) )
662 LOG_UPDATE_RENDER_TRACE_FMT( "Deleting Surface" );
664 mCore.SurfaceDeleted( deletedSurface );
669 AddPerformanceMarker( PerformanceInterface::RENDER_END );
673 // Trigger event thread to request Update/Render thread to sleep if update not required
674 if( ( Integration::KeepUpdating::NOT_REQUESTED == keepUpdatingStatus ) && !renderStatus.NeedsUpdate() )
676 mSleepTrigger->Trigger();
677 updateRequired = false;
678 LOG_UPDATE_RENDER( "Sleep Triggered" );
682 updateRequired = true;
685 //////////////////////////////
687 //////////////////////////////
689 extraFramesDropped = 0;
691 if (timeToSleepUntil == 0)
693 // If this is the first frame after the thread is initialized or resumed, we
694 // use the actual time the current frame starts from to calculate the time to
695 // sleep until the next frame.
696 timeToSleepUntil = currentFrameStartTime + mDefaultFrameDurationNanoseconds;
700 // Otherwise, always use the sleep-until time calculated in the last frame to
701 // calculate the time to sleep until the next frame. In this way, if there is
702 // any time gap between the current frame and the next frame, or if update or
703 // rendering in the current frame takes too much time so that the specified
704 // sleep-until time has already passed, it will try to keep the frames syncing
705 // by shortening the duration of the next frame.
706 timeToSleepUntil += mDefaultFrameDurationNanoseconds;
708 // Check the current time at the end of the frame
709 uint64_t currentFrameEndTime = 0;
710 TimeService::GetNanoseconds( currentFrameEndTime );
711 while ( currentFrameEndTime > timeToSleepUntil + mDefaultFrameDurationNanoseconds )
713 // We are more than one frame behind already, so just drop the next frames
714 // until the sleep-until time is later than the current time so that we can
716 timeToSleepUntil += mDefaultFrameDurationNanoseconds;
717 extraFramesDropped++;
721 // Render to FBO is intended to measure fps above 60 so sleep is not wanted.
722 if( 0u == renderToFboInterval )
724 // Sleep until at least the the default frame duration has elapsed. This will return immediately if the specified end-time has already passed.
725 TimeService::SleepUntil( timeToSleepUntil );
729 // Inform core of context destruction & shutdown EGL
730 mCore.ContextDestroyed();
731 currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
734 currentSurface->DestroySurface();
735 currentSurface = nullptr;
738 LOG_UPDATE_RENDER( "THREAD DESTROYED" );
740 // Uninstall the logging function
741 mEnvironmentOptions.UnInstallLogFunction();
744 bool CombinedUpdateRenderController::UpdateRenderReady( bool& useElapsedTime, bool updateRequired, uint64_t& timeToSleepUntil )
746 useElapsedTime = true;
748 ConditionalWait::ScopedLock updateLock( mUpdateRenderThreadWaitCondition );
749 while( ( ! mUpdateRenderRunCount || // Should try to wait if event-thread has paused the Update/Render thread
750 ( mUpdateRenderThreadCanSleep && ! updateRequired && ! mPendingRequestUpdate ) ) && // Ensure we wait if we're supposed to be sleeping AND do not require another update
751 ! mDestroyUpdateRenderThread && // Ensure we don't wait if the update-render-thread is supposed to be destroyed
752 ! mNewSurface && // Ensure we don't wait if we need to replace the surface
753 ! mDeletedSurface && // Ensure we don't wait if we need to delete the surface
754 ! mSurfaceResized ) // Ensure we don't wait if we need to resize the surface
756 LOG_UPDATE_RENDER( "WAIT: mUpdateRenderRunCount: %d", mUpdateRenderRunCount );
757 LOG_UPDATE_RENDER( " mUpdateRenderThreadCanSleep: %d, updateRequired: %d, mPendingRequestUpdate: %d", mUpdateRenderThreadCanSleep, updateRequired, mPendingRequestUpdate );
758 LOG_UPDATE_RENDER( " mDestroyUpdateRenderThread: %d", mDestroyUpdateRenderThread );
759 LOG_UPDATE_RENDER( " mNewSurface: %d", mNewSurface );
760 LOG_UPDATE_RENDER( " mDeletedSurface: %d", mDeletedSurface );
761 LOG_UPDATE_RENDER( " mSurfaceResized: %d", mSurfaceResized );
763 // Reset the time when the thread is waiting, so the sleep-until time for
764 // the first frame after resuming should be based on the actual start time
765 // of the first frame.
766 timeToSleepUntil = 0;
768 mUpdateRenderThreadWaitCondition.Wait( updateLock );
770 if( ! mUseElapsedTimeAfterWait )
772 useElapsedTime = false;
776 LOG_COUNTER_UPDATE_RENDER( "mUpdateRenderRunCount: %d", mUpdateRenderRunCount );
777 LOG_COUNTER_UPDATE_RENDER( "mUpdateRenderThreadCanSleep: %d, updateRequired: %d, mPendingRequestUpdate: %d", mUpdateRenderThreadCanSleep, updateRequired, mPendingRequestUpdate );
778 LOG_COUNTER_UPDATE_RENDER( "mDestroyUpdateRenderThread: %d", mDestroyUpdateRenderThread );
779 LOG_COUNTER_UPDATE_RENDER( "mNewSurface: %d", mNewSurface );
780 LOG_COUNTER_UPDATE_RENDER( "mDeletedSurface: %d", mDeletedSurface );
781 LOG_COUNTER_UPDATE_RENDER( "mSurfaceResized: %d", mSurfaceResized );
783 mUseElapsedTimeAfterWait = FALSE;
784 mUpdateRenderThreadCanSleep = FALSE;
785 mPendingRequestUpdate = FALSE;
787 // If we've been asked to run Update/Render cycles a finite number of times then decrement so we wait after the
788 // requested number of cycles
789 if( mUpdateRenderRunCount > 0 )
791 --mUpdateRenderRunCount;
794 // Keep the update-render thread alive if this thread is NOT to be destroyed
795 return ! mDestroyUpdateRenderThread;
798 Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
800 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
802 Integration::RenderSurface* newSurface = mNewSurface;
808 void CombinedUpdateRenderController::SurfaceReplaced()
810 // Just increment the semaphore
811 sem_post( &mEventThreadSemaphore );
814 Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeDeleted()
816 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
818 Integration::RenderSurface* deletedSurface = mDeletedSurface;
819 mDeletedSurface = NULL;
821 return deletedSurface;
824 void CombinedUpdateRenderController::SurfaceDeleted()
826 // Just increment the semaphore
827 sem_post( &mEventThreadSemaphore );
830 bool CombinedUpdateRenderController::ShouldSurfaceBeResized()
832 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
833 return mSurfaceResized;
836 void CombinedUpdateRenderController::SurfaceResized()
838 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
839 mSurfaceResized = FALSE;
842 ///////////////////////////////////////////////////////////////////////////////////////////////////
844 ///////////////////////////////////////////////////////////////////////////////////////////////////
846 void CombinedUpdateRenderController::NotifyThreadInitialised()
848 // Just increment the semaphore
849 sem_post( &mEventThreadSemaphore );
852 void CombinedUpdateRenderController::AddPerformanceMarker( PerformanceInterface::MarkerType type )
854 if( mPerformanceInterface )
856 mPerformanceInterface->AddMarker( type );
860 /////////////////////////////////////////////////////////////////////////////////////////////////
861 // POST RENDERING: EVENT THREAD
862 /////////////////////////////////////////////////////////////////////////////////////////////////
864 void CombinedUpdateRenderController::PostRenderComplete()
866 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
867 mPostRendering = FALSE;
868 mUpdateRenderThreadWaitCondition.Notify( lock );
871 ///////////////////////////////////////////////////////////////////////////////////////////////////
872 // POST RENDERING: RENDER THREAD
873 ///////////////////////////////////////////////////////////////////////////////////////////////////
875 void CombinedUpdateRenderController::PostRenderStarted()
877 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
878 mPostRendering = TRUE;
881 void CombinedUpdateRenderController::PostRenderWaitForCompletion()
883 ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
884 while( mPostRendering &&
885 ! mNewSurface && // We should NOT wait if we're replacing the surface
886 ! mDeletedSurface && // We should NOT wait if we're deleting the surface
887 ! mSurfaceResized && // We should NOT wait if we're resizing the surface
888 ! mDestroyUpdateRenderThread )
890 mUpdateRenderThreadWaitCondition.Wait( lock );
894 } // namespace Adaptor
896 } // namespace Internal