/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// EXTERNAL INCLUDES
#include <errno.h>
#include <dali/integration-api/platform-abstraction.h>
+#include <unistd.h>
// INTERNAL INCLUDES
-#include <dali/integration-api/trigger-event-factory.h>
-#include <dali/internal/adaptor/common/combined-update-render-controller-debug.h>
-#include <dali/internal/system/common/environment-options.h>
-#include <dali/internal/system/common/time-service.h>
-#include <dali/internal/adaptor/common/adaptor-internal-services.h>
+#include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
#include <dali/devel-api/adaptor-framework/thread-settings.h>
+#include <dali/internal/adaptor/common/adaptor-internal-services.h>
+#include <dali/internal/adaptor/common/combined-update-render-controller-debug.h>
#include <dali/internal/graphics/gles/egl-graphics.h>
#include <dali/internal/graphics/gles/egl-implementation.h>
#include <dali/internal/graphics/common/graphics-interface.h>
+#include <dali/internal/system/common/environment-options.h>
+#include <dali/internal/system/common/time-service.h>
+#include <dali/internal/window-system/common/window-impl.h>
namespace Dali
{
// EVENT THREAD
///////////////////////////////////////////////////////////////////////////////////////////////////
-CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions )
+CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions, ThreadMode threadMode )
: mFpsTracker( environmentOptions ),
mUpdateStatusLogger( environmentOptions ),
mEventThreadSemaphore(),
+ mGraphicsInitializeSemaphore(),
mUpdateRenderThreadWaitCondition(),
mAdaptorInterfaces( adaptorInterfaces ),
mPerformanceInterface( adaptorInterfaces.GetPerformanceInterface() ),
mDefaultHalfFrameNanoseconds( 0u ),
mUpdateRequestCount( 0u ),
mRunning( FALSE ),
+ mThreadMode( threadMode ),
mUpdateRenderRunCount( 0 ),
mDestroyUpdateRenderThread( FALSE ),
mUpdateRenderThreadCanSleep( FALSE ),
mDeletedSurface( nullptr ),
mPostRendering( FALSE ),
mSurfaceResized( FALSE ),
- mForceClear( FALSE )
+ mForceClear( FALSE ),
+ mUploadWithoutRendering( FALSE ),
+ mFirstFrameAfterResume( FALSE )
{
LOG_EVENT_TRACE;
currentSurface->SetThreadSynchronization( *this );
}
- TriggerEventFactoryInterface& triggerFactory = mAdaptorInterfaces.GetTriggerEventFactoryInterface();
- mSleepTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &CombinedUpdateRenderController::ProcessSleepRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
+ mSleepTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &CombinedUpdateRenderController::ProcessSleepRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
- sem_init( &mEventThreadSemaphore, 0, 0 ); // Initialize to 0 so that it just waits if sem_post has not been called
+ // Initialize to 0 so that it just waits if sem_post has not been called
+ sem_init( &mEventThreadSemaphore, 0, 0 );
+ sem_init( &mGraphicsInitializeSemaphore, 0, 0 );
}
CombinedUpdateRenderController::~CombinedUpdateRenderController()
sem_wait( &mEventThreadSemaphore );
}
- Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+ Dali::RenderSurfaceInterface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
if( currentSurface )
{
currentSurface->StartRender();
LOG_EVENT( "Startup Complete, starting Update/Render Thread" );
- RunUpdateRenderThread( CONTINUOUS, false /* No animation progression */ );
+ RunUpdateRenderThread( CONTINUOUS, AnimationProgression::NONE, UpdateMode::NORMAL );
DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Start\n" );
}
{
LOG_EVENT( "Resuming" );
- RunUpdateRenderThread( CONTINUOUS, true /* Animation progression required while we were paused */ );
+ RunUpdateRenderThread( CONTINUOUS, AnimationProgression::USE_ELAPSED_TIME, UpdateMode::NORMAL );
AddPerformanceMarker( PerformanceInterface::RESUME );
mRunning = TRUE;
mForceClear = TRUE;
+ mFirstFrameAfterResume = TRUE;
DALI_LOG_RELEASE_INFO( "CombinedUpdateRenderController::Resume\n" );
}
LOG_EVENT_TRACE;
// Stop Rendering and the Update/Render Thread
- Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+ Dali::RenderSurfaceInterface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
if( currentSurface )
{
currentSurface->StopRender();
{
LOG_EVENT( "Processing" );
- RunUpdateRenderThread( CONTINUOUS, false /* No animation progression */ );
+ RunUpdateRenderThread( CONTINUOUS, AnimationProgression::NONE, UpdateMode::NORMAL );
}
ConditionalWait::ScopedLock updateLock( mUpdateRenderThreadWaitCondition );
mPendingRequestUpdate = TRUE;
}
-void CombinedUpdateRenderController::RequestUpdateOnce()
+void CombinedUpdateRenderController::RequestUpdateOnce( UpdateMode updateMode )
{
// Increment the update-request count to the maximum
if( mUpdateRequestCount < MAXIMUM_UPDATE_REQUESTS )
++mUpdateRequestCount;
}
- if( IsUpdateRenderThreadPaused() )
+ if( IsUpdateRenderThreadPaused() || updateMode == UpdateMode::FORCE_RENDER )
{
LOG_EVENT_TRACE;
// Run Update/Render once
- RunUpdateRenderThread( ONCE, false /* No animation progression */ );
+ RunUpdateRenderThread( ONCE, AnimationProgression::NONE, updateMode );
}
}
}
}
+void CombinedUpdateRenderController::WaitForGraphicsInitialization()
+{
+ LOG_EVENT_TRACE;
+
+ if( mUpdateRenderThread )
+ {
+ LOG_EVENT( "Waiting for graphics initialisation, event-thread blocked" );
+
+ // Wait until the graphics has been initialised
+ sem_wait( &mGraphicsInitializeSemaphore );
+
+ LOG_EVENT( "graphics initialised, event-thread continuing" );
+ }
+}
+
void CombinedUpdateRenderController::ResizeSurface()
{
LOG_EVENT_TRACE;
mPreRenderCallback = callback;
}
+void CombinedUpdateRenderController::AddSurface( Dali::RenderSurfaceInterface* surface )
+{
+ LOG_EVENT_TRACE;
+ LOG_EVENT( "Surface is added" );
+ if( mUpdateRenderThread )
+ {
+ // Set the ThreadSyncronizationInterface on the added surface
+ surface->SetThreadSynchronization( *this );
+ }
+}
+
///////////////////////////////////////////////////////////////////////////////////////////////////
// EVENT THREAD
///////////////////////////////////////////////////////////////////////////////////////////////////
-void CombinedUpdateRenderController::RunUpdateRenderThread( int numberOfCycles, bool useElapsedTime )
+void CombinedUpdateRenderController::RunUpdateRenderThread( int numberOfCycles, AnimationProgression animationProgression, UpdateMode updateMode )
{
ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
- mUpdateRenderRunCount = numberOfCycles;
+
+ switch( mThreadMode )
+ {
+ case ThreadMode::NORMAL:
+ {
+ mUpdateRenderRunCount = numberOfCycles;
+ mUseElapsedTimeAfterWait = ( animationProgression == AnimationProgression::USE_ELAPSED_TIME );
+ break;
+ }
+ case ThreadMode::RUN_IF_REQUESTED:
+ {
+ if( updateMode != UpdateMode::FORCE_RENDER )
+ {
+ // Render only if the update mode is FORCE_RENDER which means the application requests it.
+ // We don't want to awake the update thread.
+ return;
+ }
+
+ mUpdateRenderRunCount++; // Increase the update request count
+ mUseElapsedTimeAfterWait = TRUE; // The elapsed time should be used. We want animations to proceed.
+ break;
+ }
+ }
+
mUpdateRenderThreadCanSleep = FALSE;
- mUseElapsedTimeAfterWait = useElapsedTime;
+ mUploadWithoutRendering = ( updateMode == UpdateMode::SKIP_RENDER );
LOG_COUNTER_EVENT( "mUpdateRenderRunCount: %d, mUseElapsedTimeAfterWait: %d", mUpdateRenderRunCount, mUseElapsedTimeAfterWait );
mUpdateRenderThreadWaitCondition.Notify( lock );
}
bool CombinedUpdateRenderController::IsUpdateRenderThreadPaused()
{
ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
+
+ if( mThreadMode == ThreadMode::RUN_IF_REQUESTED )
+ {
+ return !mRunning || mUpdateRenderThreadCanSleep;
+ }
+
return ( mUpdateRenderRunCount != CONTINUOUS ) || // Report paused if NOT continuously running
mUpdateRenderThreadCanSleep; // Report paused if sleeping
}
Dali::DisplayConnection& displayConnection = mAdaptorInterfaces.GetDisplayConnectionInterface();
displayConnection.Initialize();
+ // EGL has been initialised at this point
+ NotifyGraphicsInitialised();
+
RenderSurfaceInterface* currentSurface = nullptr;
GraphicsInterface& graphics = mAdaptorInterfaces.GetGraphicsInterface();
}
}
+ eglGraphics->GetGlesInterface().ContextCreated();
+
// Tell core it has a context
mCore.ContextCreated();
uint64_t currentFrameStartTime = 0;
TimeService::GetNanoseconds( currentFrameStartTime );
- const uint64_t timeSinceLastFrame = currentFrameStartTime - lastFrameTime;
+ uint64_t timeSinceLastFrame = currentFrameStartTime - lastFrameTime;
// Optional FPS Tracking when continuously rendering
if( useElapsedTime && mFpsTracker.Enabled() )
// REPLACE SURFACE
//////////////////////////////
- Integration::RenderSurface* newSurface = ShouldSurfaceBeReplaced();
+ Dali::RenderSurfaceInterface* newSurface = ShouldSurfaceBeReplaced();
if( DALI_UNLIKELY( newSurface ) )
{
LOG_UPDATE_RENDER_TRACE_FMT( "Replacing Surface" );
-
// This is designed for replacing pixmap surfaces, but should work for window as well
// we need to delete the surface and renderable (pixmap / window)
// Then create a new pixmap/window and new surface
// If the new surface has a different display connection, then the context will be lost
-
mAdaptorInterfaces.GetDisplayConnectionInterface().Initialize();
newSurface->InitializeGraphics();
- newSurface->ReplaceGraphicsSurface();
+ newSurface->MakeContextCurrent();
+ // TODO: ReplaceGraphicsSurface doesn't work, InitializeGraphics()
+ // already creates new surface window, the surface and the context.
+ // We probably don't need ReplaceGraphicsSurface at all.
+ // newSurface->ReplaceGraphicsSurface();
SurfaceReplaced();
}
float frameDelta = 0.0f;
if( useElapsedTime )
{
+ if( mThreadMode == ThreadMode::RUN_IF_REQUESTED )
+ {
+ extraFramesDropped = 0;
+ while( timeSinceLastFrame >= mDefaultFrameDurationNanoseconds )
+ {
+ timeSinceLastFrame -= mDefaultFrameDurationNanoseconds;
+ extraFramesDropped++;
+ }
+ }
+
// If using the elapsed time, then calculate frameDelta as a multiple of mDefaultFrameDelta
noOfFramesSinceLastUpdate += extraFramesDropped;
}
// Check resize
+ bool surfaceResized = false;
bool shouldSurfaceBeResized = ShouldSurfaceBeResized();
if( DALI_UNLIKELY( shouldSurfaceBeResized ) )
{
{
LOG_UPDATE_RENDER_TRACE_FMT( "Resizing Surface" );
SurfaceResized();
+ surfaceResized = true;
}
}
eglImpl.MakeContextCurrent( EGL_NO_SURFACE, eglImpl.GetContext() );
}
+ if( mFirstFrameAfterResume )
+ {
+ // mFirstFrameAfterResume is set to true when the thread is resumed
+ // Let eglImplementation know the first frame after thread initialized or resumed.
+ eglImpl.SetFirstFrameAfterResume();
+ mFirstFrameAfterResume = FALSE;
+ }
+
Integration::RenderStatus renderStatus;
AddPerformanceMarker( PerformanceInterface::RENDER_START );
- mCore.Render( renderStatus, mForceClear );
+
+ // Upload shared resources
+ mCore.PreRender( renderStatus, mForceClear, mUploadWithoutRendering );
+
+ if ( !mUploadWithoutRendering )
+ {
+ // Go through each window
+ WindowContainer windows;
+ mAdaptorInterfaces.GetWindowContainerInterface( windows );
+
+ for( auto&& window : windows )
+ {
+ Dali::Integration::Scene scene = window->GetScene();
+ Dali::RenderSurfaceInterface* windowSurface = window->GetSurface();
+
+ if ( scene && windowSurface )
+ {
+ Integration::RenderStatus windowRenderStatus;
+
+ windowSurface->InitializeGraphics();
+
+ // clear previous frame damaged render items rects, buffer history is tracked on surface level
+ mDamagedRects.clear();
+
+ // Collect damage rects
+ mCore.PreRender( scene, mDamagedRects );
+
+ // Render off-screen frame buffers first if any
+ mCore.RenderScene( windowRenderStatus, scene, true );
+
+ Rect<int> clippingRect; // Empty for fbo rendering
+
+ // Switch to the EGL context of the surface, merge damaged areas for previous frames
+ windowSurface->PreRender( surfaceResized, mDamagedRects, clippingRect ); // Switch GL context
+
+ if (clippingRect.IsEmpty())
+ {
+ mDamagedRects.clear();
+ }
+
+ // Render the surface
+ mCore.RenderScene( windowRenderStatus, scene, false, clippingRect );
+
+ if( windowRenderStatus.NeedsPostRender() )
+ {
+ windowSurface->PostRender( false, false, surfaceResized, mDamagedRects ); // Swap Buffer with damage
+ }
+ }
+ }
+ }
+
+ mCore.PostRender( mUploadWithoutRendering );
//////////////////////////////
// DELETE SURFACE
//////////////////////////////
- Integration::RenderSurface* deletedSurface = ShouldSurfaceBeDeleted();
+ Dali::RenderSurfaceInterface* deletedSurface = ShouldSurfaceBeDeleted();
if( DALI_UNLIKELY( deletedSurface ) )
{
LOG_UPDATE_RENDER_TRACE_FMT( "Deleting Surface" );
- mCore.SurfaceDeleted( deletedSurface );
+ deletedSurface->DestroySurface();
SurfaceDeleted();
}
}
}
- // Inform core of context destruction & shutdown EGL
+ // Inform core of context destruction
mCore.ContextDestroyed();
- currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
- if( currentSurface )
+
+ WindowContainer windows;
+ mAdaptorInterfaces.GetWindowContainerInterface( windows );
+
+ // Destroy surfaces
+ for( auto&& window : windows )
{
- currentSurface->DestroySurface();
- currentSurface = nullptr;
+ Dali::RenderSurfaceInterface* surface = window->GetSurface();
+ surface->DestroySurface();
}
+ // Shutdown EGL
+ eglInterface->TerminateGles();
+
LOG_UPDATE_RENDER( "THREAD DESTROYED" );
// Uninstall the logging function
return ! mDestroyUpdateRenderThread;
}
-Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
+Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
{
ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
- Integration::RenderSurface* newSurface = mNewSurface;
+ Dali::RenderSurfaceInterface* newSurface = mNewSurface;
mNewSurface = NULL;
return newSurface;
sem_post( &mEventThreadSemaphore );
}
-Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeDeleted()
+Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeDeleted()
{
ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
- Integration::RenderSurface* deletedSurface = mDeletedSurface;
+ Dali::RenderSurfaceInterface* deletedSurface = mDeletedSurface;
mDeletedSurface = NULL;
return deletedSurface;
sem_post( &mEventThreadSemaphore );
}
+void CombinedUpdateRenderController::NotifyGraphicsInitialised()
+{
+ sem_post( &mGraphicsInitializeSemaphore );
+}
+
void CombinedUpdateRenderController::AddPerformanceMarker( PerformanceInterface::MarkerType type )
{
if( mPerformanceInterface )
while( mPostRendering &&
! mNewSurface && // We should NOT wait if we're replacing the surface
! mDeletedSurface && // We should NOT wait if we're deleting the surface
- ! mSurfaceResized && // We should NOT wait if we're resizing the surface
! mDestroyUpdateRenderThread )
{
mUpdateRenderThreadWaitCondition.Wait( lock );