2 * Copyright (c) 2016 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 "vsync-notifier.h"
23 #include <dali/integration-api/core.h>
24 #include <dali/integration-api/platform-abstraction.h>
27 #include <base/interfaces/adaptor-internal-services.h>
28 #include <base/separate-update-render/thread-synchronization.h>
29 #include <base/environment-options.h>
30 #include <base/time-service.h>
44 const unsigned int NANOSECONDS_PER_SECOND( 1e+9 );
45 const unsigned int NANOSECONDS_PER_MICROSECOND( 1000u );
46 const unsigned int MICROSECONDS_PER_SECOND( 1000000u );
47 const unsigned int TIME_PER_FRAME_IN_MICROSECONDS( 16667u );
49 #if defined(DEBUG_ENABLED)
50 Integration::Log::Filter* gSyncLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_NOTIFIER");
53 } // unnamed namespace
55 VSyncNotifier::VSyncNotifier( ThreadSynchronization& sync,
56 AdaptorInternalServices& adaptorInterfaces,
57 const EnvironmentOptions& environmentOptions )
58 : mThreadSynchronization( sync ),
59 mCore( adaptorInterfaces.GetCore() ),
60 mVSyncMonitor( adaptorInterfaces.GetVSyncMonitorInterface() ),
62 mEnvironmentOptions( environmentOptions ),
63 mNumberOfVSyncsPerRender(1)
67 VSyncNotifier::~VSyncNotifier()
69 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
74 void VSyncNotifier::Start()
76 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
80 mVSyncMonitor->Initialize();
82 mThread = new pthread_t();
83 int error = pthread_create( mThread, NULL, InternalThreadEntryFunc, this );
84 DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() in VSyncNotifier" );
88 void VSyncNotifier::Stop()
90 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
94 // wait for the thread to finish
95 pthread_join(*mThread, NULL);
101 mVSyncMonitor->Terminate();
104 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106 // The following is executed inside the notifier thread !!!
107 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110 void VSyncNotifier::Run()
112 // install a function for logging
113 mEnvironmentOptions.InstallLogFunction();
115 unsigned int frameNumber( 0u ); // frameCount, updated when the thread is paused
116 unsigned int currentSequenceNumber( 0u ); // platform specific vsync sequence number (increments with each vsync)
117 unsigned int currentSeconds( 0u ); // timestamp at latest sync
118 unsigned int currentMicroseconds( 0u ); // timestamp at latest sync
119 uint64_t seconds( 0u );
120 uint64_t microseconds( 0u );
122 bool validSync( true );
123 while( mThreadSynchronization.VSyncReady( validSync, frameNumber++, currentSeconds, currentMicroseconds, mNumberOfVSyncsPerRender ) )
125 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 1 SyncWithUpdateAndRender(frame#:%d, current Sec:%u current uSec:%u)\n", frameNumber-1, currentSeconds, currentMicroseconds);
127 // Hardware VSyncs available?
128 if( mVSyncMonitor->UseHardware() )
130 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start hardware sync (%d frames) \n", mNumberOfVSyncsPerRender);
132 for( unsigned int i=0; i<mNumberOfVSyncsPerRender; ++i )
134 // Yes..wait for N hardware VSync ticks
135 validSync = mVSyncMonitor->DoSync( currentSequenceNumber, currentSeconds, currentMicroseconds );
140 // No..use software timer
141 uint64_t nanoseconds = 0;
142 TimeService::GetNanoseconds( nanoseconds );
144 seconds = nanoseconds / NANOSECONDS_PER_SECOND; // Convert to seconds
145 nanoseconds -= seconds * NANOSECONDS_PER_SECOND; // Only want remainder nanoseconds
146 microseconds = nanoseconds / NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
148 unsigned int timeDelta( MICROSECONDS_PER_SECOND * (seconds - currentSeconds) );
149 if( microseconds < currentMicroseconds)
151 timeDelta += (microseconds + MICROSECONDS_PER_SECOND) - currentMicroseconds;
155 timeDelta += microseconds - currentMicroseconds;
158 currentSeconds = seconds;
159 currentMicroseconds = microseconds;
161 unsigned int sleepTimeInMicroseconds = 0;
163 if( timeDelta < TIME_PER_FRAME_IN_MICROSECONDS )
165 sleepTimeInMicroseconds = TIME_PER_FRAME_IN_MICROSECONDS - timeDelta;
167 sleepTimeInMicroseconds += mNumberOfVSyncsPerRender * TIME_PER_FRAME_IN_MICROSECONDS;
169 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start software sync (%d frames, %u microseconds) \n", mNumberOfVSyncsPerRender, sleepTimeInMicroseconds);
172 sleepTime.tv_sec = 0;
173 sleepTime.tv_nsec = sleepTimeInMicroseconds;
174 sleepTime.tv_nsec *= NANOSECONDS_PER_MICROSECOND;
175 nanosleep( &sleepTime, NULL );
177 mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::VSYNC );
180 // uninstall a function for logging
181 mEnvironmentOptions.UnInstallLogFunction();
185 } // namespace Adaptor
187 } // namespace Internal