Replaced UpdateRender with Thread for Controller and Sync
[platform/core/uifw/dali-adaptor.git] / adaptors / base / thread-controller.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 "thread-controller.h"
20
21 // INTERNAL INCLUDES
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>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 ThreadController::ThreadController( AdaptorInternalServices& adaptorInterfaces,
39                                                 const EnvironmentOptions& environmentOptions )
40 : mAdaptorInterfaces( adaptorInterfaces ),
41   mUpdateThread( NULL ),
42   mRenderThread( NULL ),
43   mVSyncNotifier( NULL ),
44   mThreadSync( NULL ),
45   mNumberOfVSyncsPerRender( 1 )
46 {
47   mThreadSync = new ThreadSynchronization( adaptorInterfaces, mNumberOfVSyncsPerRender );
48
49   mUpdateThread = new UpdateThread( *mThreadSync, adaptorInterfaces, environmentOptions );
50
51   mRenderThread = new RenderThread( *mThreadSync, adaptorInterfaces, environmentOptions );
52
53   mVSyncNotifier = new VSyncNotifier( *mThreadSync, adaptorInterfaces, environmentOptions );
54 }
55
56 ThreadController::~ThreadController()
57 {
58   delete mVSyncNotifier;
59   delete mRenderThread;
60   delete mUpdateThread;
61   delete mThreadSync;
62 }
63
64 void ThreadController::Start()
65 {
66   // Notify the synchronization object before starting the threads
67   mThreadSync->Start();
68
69   mUpdateThread->Start();
70   mRenderThread->Start();
71   mVSyncNotifier->Start();
72 }
73
74 void ThreadController::Pause()
75 {
76   mThreadSync->Pause();
77
78   // if update thread is napping, wake it up to get it to pause in correct place
79   mThreadSync->UpdateRequested();
80 }
81
82 void ThreadController::ResumeFrameTime()
83 {
84   mThreadSync->ResumeFrameTime();
85 }
86
87 void ThreadController::Resume()
88 {
89   mThreadSync->Resume();
90 }
91
92 void ThreadController::Stop()
93 {
94   // Notify the synchronization object before stopping the threads
95   mThreadSync->Stop();
96
97   mVSyncNotifier->Stop();
98   mUpdateThread->Stop();
99   mRenderThread->Stop();
100 }
101
102 void ThreadController::RequestUpdate()
103 {
104   mThreadSync->UpdateRequested();
105 }
106
107 void ThreadController::RequestUpdateOnce()
108 {
109   // we may be sleeping
110   mThreadSync->UpdateRequested();
111   // if we are paused, need to allow one update
112   mThreadSync->UpdateWhilePaused();
113 }
114
115 void ThreadController::ReplaceSurface( RenderSurface* newSurface )
116 {
117   // tell render thread to start the replace. This call will block until the replace
118   // has completed.
119   RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
120
121   // Ensure the current surface releases any locks to prevent deadlock.
122   currentSurface->StopRender();
123
124   mThreadSync->ReplaceSurface( newSurface );
125 }
126
127 void ThreadController::NewSurface( RenderSurface* newSurface )
128 {
129   // This API shouldn't be used when there is a current surface, but check anyway.
130   RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
131   if( currentSurface )
132   {
133     currentSurface->StopRender();
134   }
135
136   mThreadSync->NewSurface( newSurface );
137 }
138
139 void ThreadController::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender )
140 {
141   mNumberOfVSyncsPerRender = numberOfVSyncsPerRender;
142   mThreadSync->SetRenderRefreshRate(numberOfVSyncsPerRender);
143 }
144
145
146 } // namespace Adaptor
147
148 } // namespace Internal
149
150 } // namespace Dali