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 "thread-controller.h"
22 #include <base/update-thread.h>
23 #include <base/render-thread.h>
24 #include <base/thread-synchronization.h>
25 #include <base/vsync-notifier.h>
26 #include <base/interfaces/adaptor-internal-services.h>
27 #include <base/environment-options.h>
38 ThreadController::ThreadController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions )
39 : mAdaptorInterfaces( adaptorInterfaces ),
40 mUpdateThread( NULL ),
41 mRenderThread( NULL ),
42 mVSyncNotifier( NULL ),
44 mNumberOfVSyncsPerRender( 1 )
46 mThreadSync = new ThreadSynchronization( adaptorInterfaces, mNumberOfVSyncsPerRender );
48 mUpdateThread = new UpdateThread( *mThreadSync, adaptorInterfaces, environmentOptions );
50 mRenderThread = new RenderThread( *mThreadSync, adaptorInterfaces, environmentOptions );
52 mVSyncNotifier = new VSyncNotifier( *mThreadSync, adaptorInterfaces, environmentOptions );
54 // Set the thread-synchronization interface on the render-surface
55 RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
58 currentSurface->SetThreadSynchronization( *mThreadSync );
62 ThreadController::~ThreadController()
64 delete mVSyncNotifier;
70 void ThreadController::Initialize()
72 // Notify the synchronization object before starting the threads
73 mThreadSync->Initialise();
75 // We want to the threads to be set up before they start
76 mUpdateThread->Start();
77 mRenderThread->Start();
78 mVSyncNotifier->Start();
81 void ThreadController::Start()
86 void ThreadController::Pause()
91 void ThreadController::Resume()
93 mThreadSync->Resume();
96 void ThreadController::Stop()
98 // Notify the synchronization object before stopping the threads
101 mVSyncNotifier->Stop();
102 mUpdateThread->Stop();
103 mRenderThread->Stop();
106 void ThreadController::RequestUpdate()
108 mThreadSync->UpdateRequest();
111 void ThreadController::RequestUpdateOnce()
113 // if we are paused, need to allow one update
114 mThreadSync->UpdateOnce();
117 void ThreadController::ReplaceSurface( RenderSurface* newSurface )
119 // Set the thread-syncronization on the new surface
120 newSurface->SetThreadSynchronization( *mThreadSync );
122 // tell render thread to start the replace. This call will block until the replace
124 RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
126 // Ensure the current surface releases any locks to prevent deadlock.
127 currentSurface->StopRender();
129 mThreadSync->ReplaceSurface( newSurface );
132 void ThreadController::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender )
134 mNumberOfVSyncsPerRender = numberOfVSyncsPerRender;
135 mThreadSync->SetRenderRefreshRate(numberOfVSyncsPerRender);
138 } // namespace Adaptor
140 } // namespace Internal