2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "update-thread.h"
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>
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
44 } // unnamed namespace
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 ),
54 mEnvironmentOptions( environmentOptions )
58 UpdateThread::~UpdateThread()
63 void UpdateThread::Start()
65 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Start()\n");
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" );
75 void UpdateThread::Stop()
77 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Stop()\n");
80 // wait for the thread to finish
81 pthread_join(*mThread, NULL);
88 bool UpdateThread::Run()
90 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
92 // Install a function for logging
93 mEnvironmentOptions.InstallLogFunction();
95 Integration::UpdateStatus status;
96 bool runUpdate = true;
97 float lastFrameDelta( 0.0f );
98 unsigned int lastSyncTime( 0 );
99 unsigned int nextSyncTime( 0 );
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 ) )
105 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - UpdateReady(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
107 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Core.Update()\n");
109 mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_START );
110 mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
111 mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_END );
113 mFpsTracker.Track( status.SecondsFromLastFrame() );
115 unsigned int keepUpdatingStatus = status.KeepUpdating();
117 // Optional logging of update/render status
118 mUpdateStatusLogger.Log( keepUpdatingStatus );
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);
125 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - runUpdate(%d)\n", runUpdate );
127 // Reset time variables
128 lastFrameDelta = 0.0f;
133 // Uninstall the logging function
134 mEnvironmentOptions.UnInstallLogFunction();
139 } // namespace Adaptor
141 } // namespace Internal