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.
20 #include <sys/types.h>
23 #include <boost/thread.hpp>
25 #include <dali/integration-api/core.h>
26 #include <dali/integration-api/platform-abstraction.h>
29 #include "vsync-notifier.h"
30 #include <base/interfaces/adaptor-internal-services.h>
31 #include <base/update-render-synchronization.h>
32 #include <base/environment-options.h>
46 const unsigned int MICROSECONDS_PER_SECOND( 100000u );
47 const unsigned int TIME_PER_FRAME_IN_MICROSECONDS( 16667u );
49 #if defined(DEBUG_ENABLED)
50 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::Concise, false, "LOG_VSYNC_NOTIFIER");
53 } // unnamed namespace
55 VSyncNotifier::VSyncNotifier( UpdateRenderSynchronization& sync,
56 AdaptorInternalServices& adaptorInterfaces,
57 const EnvironmentOptions& environmentOptions )
58 : mUpdateRenderSync( sync ),
59 mCore( adaptorInterfaces.GetCore() ),
60 mPlatformAbstraction( adaptorInterfaces.GetPlatformAbstractionInterface() ),
61 mVSyncMonitor( adaptorInterfaces.GetVSyncMonitorInterface() ),
63 mEnvironmentOptions( environmentOptions )
67 VSyncNotifier::~VSyncNotifier()
69 DALI_LOG_INFO( gLogFilter, Debug::General, "%s\n", __func__ );
74 void VSyncNotifier::Start()
76 DALI_LOG_INFO( gLogFilter, Debug::General, "%s\n", __func__ );
80 mVSyncMonitor->Initialize();
82 mThread = new boost::thread( boost::bind( &VSyncNotifier::Run, this ) );
86 void VSyncNotifier::Stop()
88 DALI_LOG_INFO( gLogFilter, Debug::General, "%s\n", __func__ );
92 // wait for the thread to finish
99 mVSyncMonitor->Terminate();
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 // The following is executed inside the notifier thread !!!
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108 void VSyncNotifier::Run()
110 // install a function for logging
111 mEnvironmentOptions.InstallLogFunction();
113 unsigned int frameNumber( 0u ); // frameCount, updated when the thread is paused
114 unsigned int currentSequenceNumber( 0u ); // platform specific vsync sequence number (increments with each vsync)
115 unsigned int currentSeconds( 0u ); // timestamp at latest vsync
116 unsigned int currentMicroseconds( 0u ); // timestamp at latest vsync
117 unsigned int seconds( 0u );
118 unsigned int microseconds( 0u );
120 bool running( true );
123 bool validSync( true );
125 // Hardware VSyncs available?
126 if( mVSyncMonitor->UseHardware() )
128 // Yes..wait for hardware VSync
129 validSync = mVSyncMonitor->DoSync( currentSequenceNumber, currentSeconds, currentMicroseconds );
133 // No..use software timer
134 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
136 unsigned int timeDelta( MICROSECONDS_PER_SECOND * (seconds - currentSeconds) );
137 if( microseconds < currentMicroseconds)
139 timeDelta += (microseconds + MICROSECONDS_PER_SECOND) - currentMicroseconds;
143 timeDelta += microseconds - currentMicroseconds;
146 if( timeDelta < TIME_PER_FRAME_IN_MICROSECONDS )
148 usleep( TIME_PER_FRAME_IN_MICROSECONDS - timeDelta );
152 usleep( TIME_PER_FRAME_IN_MICROSECONDS );
158 // call Core::VSync with frame number and time stamp
159 mCore.VSync( ++frameNumber, currentSeconds, currentMicroseconds );
162 running = mUpdateRenderSync.VSyncNotifierSyncWithUpdateAndRender( frameNumber, currentSeconds, currentMicroseconds );
165 // uninstall a function for logging
166 mEnvironmentOptions.UnInstallLogFunction();
170 } // namespace Adaptor
172 } // namespace Internal