Remove de-funct Bullet dynamics plugin
[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 #include <dali/integration-api/core.h>
22 #include <dali/integration-api/platform-abstraction.h>
23
24 // INTERNAL INCLUDES
25 #include <base/interfaces/adaptor-internal-services.h>
26 #include <base/update-render-synchronization.h>
27 #include <base/environment-options.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace
39 {
40
41 const unsigned int MICROSECONDS_PER_SECOND( 1000000u );
42 const unsigned int TIME_PER_FRAME_IN_MICROSECONDS( 16667u );
43
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gSyncLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_NOTIFIER");
46 #endif
47
48 } // unnamed namespace
49
50 VSyncNotifier::VSyncNotifier( UpdateRenderSynchronization& sync,
51                               AdaptorInternalServices& adaptorInterfaces,
52                               const EnvironmentOptions& environmentOptions )
53 : mUpdateRenderSync( sync ),
54   mCore( adaptorInterfaces.GetCore() ),
55   mPlatformAbstraction( adaptorInterfaces.GetPlatformAbstractionInterface() ),
56   mVSyncMonitor( adaptorInterfaces.GetVSyncMonitorInterface() ),
57   mThread( NULL ),
58   mEnvironmentOptions( environmentOptions ),
59   mNumberOfVSyncsPerRender(1)
60 {
61 }
62
63 VSyncNotifier::~VSyncNotifier()
64 {
65   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
66
67   Stop();
68 }
69
70 void VSyncNotifier::Start()
71 {
72   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
73
74   if ( !mThread )
75   {
76     mVSyncMonitor->Initialize();
77
78     mThread = new pthread_t();
79     int error = pthread_create( mThread, NULL, InternalThreadEntryFunc, this );
80     DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() in VSyncNotifier" );
81   }
82 }
83
84 void VSyncNotifier::Stop()
85 {
86   DALI_LOG_INFO( gSyncLogFilter, Debug::General, "%s\n", __func__ );
87
88   if( mThread )
89   {
90     // wait for the thread to finish
91     pthread_join(*mThread, NULL);
92
93     delete mThread;
94     mThread = NULL;
95   }
96
97   mVSyncMonitor->Terminate();
98 }
99
100 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
101 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 // The following is executed inside the notifier thread !!!
103 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105
106 void VSyncNotifier::Run()
107 {
108   // install a function for logging
109   mEnvironmentOptions.InstallLogFunction();
110
111   unsigned int frameNumber( 0u );             // frameCount, updated when the thread is paused
112   unsigned int currentSequenceNumber( 0u );   // platform specific vsync sequence number (increments with each vsync)
113   unsigned int currentSeconds( 0u );          // timestamp at latest sync
114   unsigned int currentMicroseconds( 0u );     // timestamp at latest sync
115   unsigned int seconds( 0u );
116   unsigned int microseconds( 0u );
117
118   bool running( true );
119   while( running )
120   {
121     DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 1 Start loop \n");
122
123     bool validSync( true );
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       usleep( sleepTimeInMicroseconds );
164     }
165
166     DALI_LOG_INFO( gSyncLogFilter, Debug::General, "VSyncNotifier::Run. 3 SyncWithUpdateAndRender(frame#:%d, current Sec:%u current uSec:%u)\n", frameNumber+1, currentSeconds, currentMicroseconds);
167
168     running = mUpdateRenderSync.VSyncNotifierSyncWithUpdateAndRender( validSync, ++frameNumber, currentSeconds, currentMicroseconds, mNumberOfVSyncsPerRender );
169     // The number of vsyncs per render may have been modified by this call.
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