Improve ThreadSynchronization Logging (enable release logging if required)
[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::Initialize()
65 {
66   // Notify the synchronization object before starting the threads
67   mThreadSync->Initialise();
68
69   // We want to the threads to be set up before they start
70   mUpdateThread->Start();
71   mRenderThread->Start();
72   mVSyncNotifier->Start();
73 }
74
75 void ThreadController::Start()
76 {
77   mThreadSync->Start();
78 }
79
80 void ThreadController::Pause()
81 {
82   mThreadSync->Pause();
83 }
84
85 void ThreadController::Resume()
86 {
87   mThreadSync->Resume();
88 }
89
90 void ThreadController::Stop()
91 {
92   // Notify the synchronization object before stopping the threads
93   mThreadSync->Stop();
94
95   mVSyncNotifier->Stop();
96   mUpdateThread->Stop();
97   mRenderThread->Stop();
98 }
99
100 void ThreadController::RequestUpdate()
101 {
102   mThreadSync->UpdateRequest();
103 }
104
105 void ThreadController::RequestUpdateOnce()
106 {
107   // if we are paused, need to allow one update
108   mThreadSync->UpdateOnce();
109 }
110
111 void ThreadController::ReplaceSurface( RenderSurface* newSurface )
112 {
113   // tell render thread to start the replace. This call will block until the replace
114   // has completed.
115   RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
116
117   // Ensure the current surface releases any locks to prevent deadlock.
118   currentSurface->StopRender();
119
120   mThreadSync->ReplaceSurface( newSurface );
121 }
122
123 void ThreadController::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender )
124 {
125   mNumberOfVSyncsPerRender = numberOfVSyncsPerRender;
126   mThreadSync->SetRenderRefreshRate(numberOfVSyncsPerRender);
127 }
128
129 } // namespace Adaptor
130
131 } // namespace Internal
132
133 } // namespace Dali