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,
39 const EnvironmentOptions& environmentOptions )
40 : mAdaptorInterfaces( adaptorInterfaces ),
41 mUpdateThread( NULL ),
42 mRenderThread( NULL ),
43 mVSyncNotifier( NULL ),
45 mNumberOfVSyncsPerRender( 1 )
47 mThreadSync = new ThreadSynchronization( adaptorInterfaces, mNumberOfVSyncsPerRender );
49 mUpdateThread = new UpdateThread( *mThreadSync, adaptorInterfaces, environmentOptions );
51 mRenderThread = new RenderThread( *mThreadSync, adaptorInterfaces, environmentOptions );
53 mVSyncNotifier = new VSyncNotifier( *mThreadSync, adaptorInterfaces, environmentOptions );
56 ThreadController::~ThreadController()
58 delete mVSyncNotifier;
64 void ThreadController::Start()
66 // Notify the synchronization object before starting the threads
69 mUpdateThread->Start();
70 mRenderThread->Start();
71 mVSyncNotifier->Start();
74 void ThreadController::Pause()
78 // if update thread is napping, wake it up to get it to pause in correct place
79 mThreadSync->UpdateRequested();
82 void ThreadController::ResumeFrameTime()
84 mThreadSync->ResumeFrameTime();
87 void ThreadController::Resume()
89 mThreadSync->Resume();
92 void ThreadController::Stop()
94 // Notify the synchronization object before stopping the threads
97 mVSyncNotifier->Stop();
98 mUpdateThread->Stop();
99 mRenderThread->Stop();
102 void ThreadController::RequestUpdate()
104 mThreadSync->UpdateRequested();
107 void ThreadController::RequestUpdateOnce()
109 // we may be sleeping
110 mThreadSync->UpdateRequested();
111 // if we are paused, need to allow one update
112 mThreadSync->UpdateWhilePaused();
115 void ThreadController::ReplaceSurface( RenderSurface* newSurface )
117 // tell render thread to start the replace. This call will block until the replace
119 RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
121 // Ensure the current surface releases any locks to prevent deadlock.
122 currentSurface->StopRender();
124 mThreadSync->ReplaceSurface( newSurface );
127 void ThreadController::NewSurface( RenderSurface* newSurface )
129 // This API shouldn't be used when there is a current surface, but check anyway.
130 RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
133 currentSurface->StopRender();
136 mThreadSync->NewSurface( newSurface );
139 void ThreadController::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender )
141 mNumberOfVSyncsPerRender = numberOfVSyncsPerRender;
142 mThreadSync->SetRenderRefreshRate(numberOfVSyncsPerRender);
146 } // namespace Adaptor
148 } // namespace Internal