a0bcd972a3087d85ec90d60325b27486cf4de9bd
[platform/core/uifw/dali-adaptor.git] / adaptors / base / separate-update-render / update-thread.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 "update-thread.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstdio>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <base/interfaces/adaptor-internal-services.h>
27 #include <base/separate-update-render/thread-synchronization.h>
28 #include <base/environment-options.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace Adaptor
37 {
38
39 namespace
40 {
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
43 #endif
44 } // unnamed namespace
45
46 UpdateThread::UpdateThread( ThreadSynchronization& sync,
47                             AdaptorInternalServices& adaptorInterfaces,
48                             const EnvironmentOptions& environmentOptions )
49 : mThreadSynchronization( sync ),
50   mCore( adaptorInterfaces.GetCore()),
51   mFpsTracker( environmentOptions ),
52   mUpdateStatusLogger( environmentOptions ),
53   mThread( NULL ),
54   mEnvironmentOptions( environmentOptions )
55 {
56 }
57
58 UpdateThread::~UpdateThread()
59 {
60   Stop();
61 }
62
63 void UpdateThread::Start()
64 {
65   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Start()\n");
66   if ( !mThread )
67   {
68     // Create and run the update-thread
69     mThread =  new pthread_t();
70     int error = pthread_create( mThread, NULL, InternalThreadEntryFunc, this );
71     DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() in UpdateThread" );
72   }
73 }
74
75 void UpdateThread::Stop()
76 {
77   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Stop()\n");
78   if( mThread )
79   {
80     // wait for the thread to finish
81     pthread_join(*mThread, NULL);
82
83     delete mThread;
84     mThread = NULL;
85   }
86 }
87
88 bool UpdateThread::Run()
89 {
90   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
91
92   // Install a function for logging
93   mEnvironmentOptions.InstallLogFunction();
94
95   Integration::UpdateStatus status;
96   bool runUpdate = true;
97   float lastFrameDelta( 0.0f );
98   unsigned int lastSyncTime( 0 );
99   unsigned int nextSyncTime( 0 );
100
101   // Update loop, we stay inside here while the update-thread is running
102   // We also get the last delta and the predict when this update will be rendered
103   while ( mThreadSynchronization.UpdateReady( status.NeedsNotification(), runUpdate, lastFrameDelta, lastSyncTime, nextSyncTime ) )
104   {
105     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - UpdateReady(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
106
107     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Core.Update()\n");
108
109     mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_START );
110     mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
111     mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_END );
112
113     mFpsTracker.Track( status.SecondsFromLastFrame() );
114
115     unsigned int keepUpdatingStatus = status.KeepUpdating();
116
117     // Optional logging of update/render status
118     mUpdateStatusLogger.Log( keepUpdatingStatus );
119
120     //  2 things can keep update running.
121     // - The status of the last update
122     // - The status of the last render
123     runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus);
124
125     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - runUpdate(%d)\n", runUpdate );
126
127     // Reset time variables
128     lastFrameDelta = 0.0f;
129     lastSyncTime = 0;
130     nextSyncTime = 0;
131   }
132
133   // Uninstall the logging function
134   mEnvironmentOptions.UnInstallLogFunction();
135
136   return true;
137 }
138
139 } // namespace Adaptor
140
141 } // namespace Internal
142
143 } // namespace Dali