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 "vsync-notifier.h"
22 #include <boost/thread.hpp>
24 #include <dali/integration-api/core.h>
25 #include <dali/integration-api/platform-abstraction.h>
28 #include <base/interfaces/adaptor-internal-services.h>
29 #include <base/update-render-synchronization.h>
30 #include <base/environment-options.h>
44 const unsigned int MICROSECONDS_PER_SECOND( 1000000u );
45 const unsigned int TIME_PER_FRAME_IN_MICROSECONDS( 16667u );
47 #if defined(DEBUG_ENABLED)
48 Integration::Log::Filter* gSyncLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_NOTIFIER");
51 } // unnamed namespace
53 VSyncNotifier::VSyncNotifier( UpdateRenderSynchronization& sync,
54 AdaptorInternalServices& adaptorInterfaces,
55 const EnvironmentOptions& environmentOptions )
56 : mUpdateRenderSync( sync ),
57 mCore( adaptorInterfaces.GetCore() ),
58 mPlatformAbstraction( adaptorInterfaces.GetPlatformAbstractionInterface() ),
59 mVSyncMonitor( adaptorInterfaces.GetVSyncMonitorInterface() ),
61 mEnvironmentOptions( environmentOptions ),
62 mNumberOfVSyncsPerRender(1)
66 VSyncNotifier::~VSyncNotifier()
68 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
73 void VSyncNotifier::Start()
75 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
79 mVSyncMonitor->Initialize();
81 mThread = new boost::thread( boost::bind( &VSyncNotifier::Run, this ) );
85 void VSyncNotifier::Stop()
87 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
91 // wait for the thread to finish
98 mVSyncMonitor->Terminate();
101 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 // The following is executed inside the notifier thread !!!
104 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107 void VSyncNotifier::Run()
109 // install a function for logging
110 mEnvironmentOptions.InstallLogFunction();
112 unsigned int frameNumber( 0u ); // frameCount, updated when the thread is paused
113 unsigned int currentSequenceNumber( 0u ); // platform specific vsync sequence number (increments with each vsync)
114 unsigned int currentSeconds( 0u ); // timestamp at latest sync
115 unsigned int currentMicroseconds( 0u ); // timestamp at latest sync
116 unsigned int seconds( 0u );
117 unsigned int microseconds( 0u );
119 bool running( true );
122 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 1 Start loop \n");
124 bool validSync( true );
126 // Hardware VSyncs available?
127 if( mVSyncMonitor->UseHardware() )
129 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start hardware sync (%d frames) \n", mNumberOfVSyncsPerRender);
131 for( unsigned int i=0; i<mNumberOfVSyncsPerRender; ++i )
133 // Yes..wait for N hardware VSync ticks
134 validSync = mVSyncMonitor->DoSync( currentSequenceNumber, currentSeconds, currentMicroseconds );
139 // No..use software timer
140 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
142 unsigned int timeDelta( MICROSECONDS_PER_SECOND * (seconds - currentSeconds) );
143 if( microseconds < currentMicroseconds)
145 timeDelta += (microseconds + MICROSECONDS_PER_SECOND) - currentMicroseconds;
149 timeDelta += microseconds - currentMicroseconds;
152 currentSeconds = seconds;
153 currentMicroseconds = microseconds;
155 unsigned int sleepTimeInMicroseconds = 0;
157 if( timeDelta < TIME_PER_FRAME_IN_MICROSECONDS )
159 sleepTimeInMicroseconds = TIME_PER_FRAME_IN_MICROSECONDS - timeDelta;
161 sleepTimeInMicroseconds += mNumberOfVSyncsPerRender * TIME_PER_FRAME_IN_MICROSECONDS;
163 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start software sync (%d frames, %u microseconds) \n", mNumberOfVSyncsPerRender, sleepTimeInMicroseconds);
164 usleep( sleepTimeInMicroseconds );
167 DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 3 SyncWithUpdateAndRender(frame#:%d, current Sec:%u current uSec:%u)\n", frameNumber+1, currentSeconds, currentMicroseconds);
169 running = mUpdateRenderSync.VSyncNotifierSyncWithUpdateAndRender( validSync, ++frameNumber, currentSeconds, currentMicroseconds, mNumberOfVSyncsPerRender );
170 // The number of vsyncs per render may have been modified by this call.
173 // uninstall a function for logging
174 mEnvironmentOptions.UnInstallLogFunction();
178 } // namespace Adaptor
180 } // namespace Internal