Merge changes Id88b7bc9,I4610f81b into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / base / separate-update-render / vsync-notifier.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "vsync-notifier.h"
20
21 // EXTERNAL INCLUDES
22 #include <unistd.h>
23 #include <dali/integration-api/core.h>
24 #include <dali/integration-api/platform-abstraction.h>
25
26 // INTERNAL INCLUDES
27 #include <base/interfaces/adaptor-internal-services.h>
28 #include <base/separate-update-render/thread-synchronization.h>
29 #include <base/environment-options.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42
43 const unsigned int NANOSECONDS_PER_MICROSECOND( 1000u );
44 const unsigned int MICROSECONDS_PER_SECOND( 1000000u );
45 const unsigned int TIME_PER_FRAME_IN_MICROSECONDS( 16667u );
46
47 #if defined(DEBUG_ENABLED)
48 Integration::Log::Filter* gSyncLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_NOTIFIER");
49 #endif
50
51 } // unnamed namespace
52
53 VSyncNotifier::VSyncNotifier( ThreadSynchronization& sync,
54                               AdaptorInternalServices& adaptorInterfaces,
55                               const EnvironmentOptions& environmentOptions )
56 : mThreadSynchronization( sync ),
57   mCore( adaptorInterfaces.GetCore() ),
58   mPlatformAbstraction( adaptorInterfaces.GetPlatformAbstractionInterface() ),
59   mVSyncMonitor( adaptorInterfaces.GetVSyncMonitorInterface() ),
60   mThread( NULL ),
61   mEnvironmentOptions( environmentOptions ),
62   mNumberOfVSyncsPerRender(1)
63 {
64 }
65
66 VSyncNotifier::~VSyncNotifier()
67 {
68   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
69
70   Stop();
71 }
72
73 void VSyncNotifier::Start()
74 {
75   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
76
77   if ( !mThread )
78   {
79     mVSyncMonitor->Initialize();
80
81     mThread = new pthread_t();
82     int error = pthread_create( mThread, NULL, InternalThreadEntryFunc, this );
83     DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() in VSyncNotifier" );
84   }
85 }
86
87 void VSyncNotifier::Stop()
88 {
89   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
90
91   if( mThread )
92   {
93     // wait for the thread to finish
94     pthread_join(*mThread, NULL);
95
96     delete mThread;
97     mThread = NULL;
98   }
99
100   mVSyncMonitor->Terminate();
101 }
102
103 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 // The following is executed inside the notifier thread !!!
106 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108
109 void VSyncNotifier::Run()
110 {
111   // install a function for logging
112   mEnvironmentOptions.InstallLogFunction();
113
114   unsigned int frameNumber( 0u );             // frameCount, updated when the thread is paused
115   unsigned int currentSequenceNumber( 0u );   // platform specific vsync sequence number (increments with each vsync)
116   unsigned int currentSeconds( 0u );              // timestamp at latest sync
117   unsigned int currentMicroseconds( 0u );         // timestamp at latest sync
118   uint64_t seconds( 0u );
119   uint64_t microseconds( 0u );
120
121   bool validSync( true );
122   while( mThreadSynchronization.VSyncReady( validSync, frameNumber++, currentSeconds, currentMicroseconds, mNumberOfVSyncsPerRender ) )
123   {
124     DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 1 SyncWithUpdateAndRender(frame#:%d, current Sec:%u current uSec:%u)\n", frameNumber-1, currentSeconds, currentMicroseconds);
125
126     // Hardware VSyncs available?
127     if( mVSyncMonitor->UseHardware() )
128     {
129       DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start hardware sync (%d frames) \n", mNumberOfVSyncsPerRender);
130
131       for( unsigned int i=0; i<mNumberOfVSyncsPerRender; ++i )
132       {
133         // Yes..wait for N hardware VSync ticks
134         validSync = mVSyncMonitor->DoSync( currentSequenceNumber, currentSeconds, currentMicroseconds );
135       }
136     }
137     else
138     {
139       // No..use software timer
140       mPlatformAbstraction.GetTimeNanoseconds( seconds, microseconds );
141       microseconds /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
142
143       unsigned int timeDelta( MICROSECONDS_PER_SECOND * (seconds - currentSeconds) );
144       if( microseconds < currentMicroseconds)
145       {
146         timeDelta += (microseconds + MICROSECONDS_PER_SECOND) - currentMicroseconds;
147       }
148       else
149       {
150         timeDelta += microseconds - currentMicroseconds;
151       }
152
153       currentSeconds = seconds;
154       currentMicroseconds = microseconds;
155
156       unsigned int sleepTimeInMicroseconds = 0;
157
158       if( timeDelta < TIME_PER_FRAME_IN_MICROSECONDS )
159       {
160         sleepTimeInMicroseconds = TIME_PER_FRAME_IN_MICROSECONDS - timeDelta;
161       }
162       sleepTimeInMicroseconds += mNumberOfVSyncsPerRender * TIME_PER_FRAME_IN_MICROSECONDS;
163
164       DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start software sync (%d frames, %u microseconds) \n", mNumberOfVSyncsPerRender, sleepTimeInMicroseconds);
165
166       timespec sleepTime;
167       sleepTime.tv_sec = 0;
168       sleepTime.tv_nsec = sleepTimeInMicroseconds * 1000;
169       nanosleep( &sleepTime, NULL );
170     }
171     mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::VSYNC );
172   }
173
174   // uninstall a function for logging
175   mEnvironmentOptions.UnInstallLogFunction();
176
177 }
178
179 } // namespace Adaptor
180
181 } // namespace Internal
182
183 } // namespace Dali