Idle callback boost removal and tidy up
[platform/core/uifw/dali-adaptor.git] / adaptors / base / vsync-notifier.cpp
1 /*
2  * Copyright (c) 2014 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 <boost/thread.hpp>
23
24 #include <dali/integration-api/core.h>
25 #include <dali/integration-api/platform-abstraction.h>
26
27 // INTERNAL INCLUDES
28 #include <base/interfaces/adaptor-internal-services.h>
29 #include <base/update-render-synchronization.h>
30 #include <base/environment-options.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40
41 namespace
42 {
43
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( UpdateRenderSynchronization& sync,
54                               AdaptorInternalServices& adaptorInterfaces,
55                               const EnvironmentOptions& environmentOptions )
56 : mUpdateRenderSync( 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 boost::thread( boost::bind( &VSyncNotifier::Run, this ) );
82   }
83 }
84
85 void VSyncNotifier::Stop()
86 {
87   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
88
89   if( mThread )
90   {
91     // wait for the thread to finish
92     mThread->join();
93
94     delete mThread;
95     mThread = NULL;
96   }
97
98   mVSyncMonitor->Terminate();
99 }
100
101 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 // The following is executed inside the notifier thread !!!
104 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106
107 void VSyncNotifier::Run()
108 {
109   // install a function for logging
110   mEnvironmentOptions.InstallLogFunction();
111
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 );
118
119   bool running( true );
120   while( running )
121   {
122     DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 1 Start loop \n");
123
124     bool validSync( true );
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.GetTimeMicroseconds( seconds, microseconds );
141
142       unsigned int timeDelta( MICROSECONDS_PER_SECOND * (seconds - currentSeconds) );
143       if( microseconds < currentMicroseconds)
144       {
145         timeDelta += (microseconds + MICROSECONDS_PER_SECOND) - currentMicroseconds;
146       }
147       else
148       {
149         timeDelta += microseconds - currentMicroseconds;
150       }
151
152       currentSeconds = seconds;
153       currentMicroseconds = microseconds;
154
155       unsigned int sleepTimeInMicroseconds = 0;
156
157       if( timeDelta < TIME_PER_FRAME_IN_MICROSECONDS )
158       {
159         sleepTimeInMicroseconds = TIME_PER_FRAME_IN_MICROSECONDS - timeDelta;
160       }
161       sleepTimeInMicroseconds += mNumberOfVSyncsPerRender * TIME_PER_FRAME_IN_MICROSECONDS;
162
163       DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 2 Start software sync (%d frames, %u microseconds) \n", mNumberOfVSyncsPerRender, sleepTimeInMicroseconds);
164       usleep( sleepTimeInMicroseconds );
165     }
166
167     DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 3 SyncWithUpdateAndRender(frame#:%d, current Sec:%u current uSec:%u)\n", frameNumber+1, currentSeconds, currentMicroseconds);
168
169     running = mUpdateRenderSync.VSyncNotifierSyncWithUpdateAndRender( validSync, ++frameNumber, currentSeconds, currentMicroseconds, mNumberOfVSyncsPerRender );
170     // The number of vsyncs per render may have been modified by this call.
171   }
172
173   // uninstall a function for logging
174   mEnvironmentOptions.UnInstallLogFunction();
175
176 }
177
178 } // namespace Adaptor
179
180 } // namespace Internal
181
182 } // namespace Dali