Moved thread control functionality into a separate folder & added threading mode...
[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 MICROSECONDS_PER_SECOND( 1000000u );
44 const unsigned int TIME_PER_FRAME_IN_MICROSECONDS( 16667u );
45
46 #if defined(DEBUG_ENABLED)
47 Integration::Log::Filter* gSyncLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_NOTIFIER");
48 #endif
49
50 } // unnamed namespace
51
52 VSyncNotifier::VSyncNotifier( ThreadSynchronization& sync,
53                               AdaptorInternalServices& adaptorInterfaces,
54                               const EnvironmentOptions& environmentOptions )
55 : mThreadSynchronization( sync ),
56   mCore( adaptorInterfaces.GetCore() ),
57   mPlatformAbstraction( adaptorInterfaces.GetPlatformAbstractionInterface() ),
58   mVSyncMonitor( adaptorInterfaces.GetVSyncMonitorInterface() ),
59   mThread( NULL ),
60   mEnvironmentOptions( environmentOptions ),
61   mNumberOfVSyncsPerRender(1)
62 {
63 }
64
65 VSyncNotifier::~VSyncNotifier()
66 {
67   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
68
69   Stop();
70 }
71
72 void VSyncNotifier::Start()
73 {
74   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
75
76   if ( !mThread )
77   {
78     mVSyncMonitor->Initialize();
79
80     mThread = new pthread_t();
81     int error = pthread_create( mThread, NULL, InternalThreadEntryFunc, this );
82     DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() in VSyncNotifier" );
83   }
84 }
85
86 void VSyncNotifier::Stop()
87 {
88   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
89
90   if( mThread )
91   {
92     // wait for the thread to finish
93     pthread_join(*mThread, NULL);
94
95     delete mThread;
96     mThread = NULL;
97   }
98
99   mVSyncMonitor->Terminate();
100 }
101
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 // The following is executed inside the notifier thread !!!
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107
108 void VSyncNotifier::Run()
109 {
110   // install a function for logging
111   mEnvironmentOptions.InstallLogFunction();
112
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 sync
116   unsigned int currentMicroseconds( 0u );     // timestamp at latest sync
117   unsigned int seconds( 0u );
118   unsigned int microseconds( 0u );
119
120   bool validSync( true );
121   while( mThreadSynchronization.VSyncReady( validSync, frameNumber++, currentSeconds, currentMicroseconds, mNumberOfVSyncsPerRender ) )
122   {
123     DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 1 SyncWithUpdateAndRender(frame#:%d, current Sec:%u current uSec:%u)\n", frameNumber-1, currentSeconds, currentMicroseconds);
124
125     // Hardware VSyncs available?
126     if( mVSyncMonitor->UseHardware() )
127     {
128       DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start hardware sync (%d frames) \n", mNumberOfVSyncsPerRender);
129
130       for( unsigned int i=0; i<mNumberOfVSyncsPerRender; ++i )
131       {
132         // Yes..wait for N hardware VSync ticks
133         validSync = mVSyncMonitor->DoSync( currentSequenceNumber, currentSeconds, currentMicroseconds );
134       }
135     }
136     else
137     {
138       // No..use software timer
139       mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
140
141       unsigned int timeDelta( MICROSECONDS_PER_SECOND * (seconds - currentSeconds) );
142       if( microseconds < currentMicroseconds)
143       {
144         timeDelta += (microseconds + MICROSECONDS_PER_SECOND) - currentMicroseconds;
145       }
146       else
147       {
148         timeDelta += microseconds - currentMicroseconds;
149       }
150
151       currentSeconds = seconds;
152       currentMicroseconds = microseconds;
153
154       unsigned int sleepTimeInMicroseconds = 0;
155
156       if( timeDelta < TIME_PER_FRAME_IN_MICROSECONDS )
157       {
158         sleepTimeInMicroseconds = TIME_PER_FRAME_IN_MICROSECONDS - timeDelta;
159       }
160       sleepTimeInMicroseconds += mNumberOfVSyncsPerRender * TIME_PER_FRAME_IN_MICROSECONDS;
161
162       DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start software sync (%d frames, %u microseconds) \n", mNumberOfVSyncsPerRender, sleepTimeInMicroseconds);
163
164       timespec sleepTime;
165       sleepTime.tv_sec = 0;
166       sleepTime.tv_nsec = sleepTimeInMicroseconds * 1000;
167       nanosleep( &sleepTime, NULL );
168     }
169     mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::VSYNC );
170   }
171
172   // uninstall a function for logging
173   mEnvironmentOptions.UnInstallLogFunction();
174
175 }
176
177 } // namespace Adaptor
178
179 } // namespace Internal
180
181 } // namespace Dali