Merge "Pixmap render surface synchronization moved to thread-synchronization" into...
[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, const EnvironmentOptions& environmentOptions )
39 : mAdaptorInterfaces( adaptorInterfaces ),
40   mUpdateThread( NULL ),
41   mRenderThread( NULL ),
42   mVSyncNotifier( NULL ),
43   mThreadSync( NULL ),
44   mNumberOfVSyncsPerRender( 1 )
45 {
46   mThreadSync = new ThreadSynchronization( adaptorInterfaces, mNumberOfVSyncsPerRender );
47
48   mUpdateThread = new UpdateThread( *mThreadSync, adaptorInterfaces, environmentOptions );
49
50   mRenderThread = new RenderThread( *mThreadSync, adaptorInterfaces, environmentOptions );
51
52   mVSyncNotifier = new VSyncNotifier( *mThreadSync, adaptorInterfaces, environmentOptions );
53
54   // Set the thread-synchronization interface on the render-surface
55   RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
56   if( currentSurface )
57   {
58     currentSurface->SetThreadSynchronization( *mThreadSync );
59   }
60 }
61
62 ThreadController::~ThreadController()
63 {
64   delete mVSyncNotifier;
65   delete mRenderThread;
66   delete mUpdateThread;
67   delete mThreadSync;
68 }
69
70 void ThreadController::Initialize()
71 {
72   // Notify the synchronization object before starting the threads
73   mThreadSync->Initialise();
74
75   // We want to the threads to be set up before they start
76   mUpdateThread->Start();
77   mRenderThread->Start();
78   mVSyncNotifier->Start();
79 }
80
81 void ThreadController::Start()
82 {
83   mThreadSync->Start();
84 }
85
86 void ThreadController::Pause()
87 {
88   mThreadSync->Pause();
89 }
90
91 void ThreadController::Resume()
92 {
93   mThreadSync->Resume();
94 }
95
96 void ThreadController::Stop()
97 {
98   // Notify the synchronization object before stopping the threads
99   mThreadSync->Stop();
100
101   mVSyncNotifier->Stop();
102   mUpdateThread->Stop();
103   mRenderThread->Stop();
104 }
105
106 void ThreadController::RequestUpdate()
107 {
108   mThreadSync->UpdateRequest();
109 }
110
111 void ThreadController::RequestUpdateOnce()
112 {
113   // if we are paused, need to allow one update
114   mThreadSync->UpdateOnce();
115 }
116
117 void ThreadController::ReplaceSurface( RenderSurface* newSurface )
118 {
119   // Set the thread-syncronization on the new surface
120   newSurface->SetThreadSynchronization( *mThreadSync );
121
122   // tell render thread to start the replace. This call will block until the replace
123   // has completed.
124   RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
125
126   // Ensure the current surface releases any locks to prevent deadlock.
127   currentSurface->StopRender();
128
129   mThreadSync->ReplaceSurface( newSurface );
130 }
131
132 void ThreadController::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender )
133 {
134   mNumberOfVSyncsPerRender = numberOfVSyncsPerRender;
135   mThreadSync->SetRenderRefreshRate(numberOfVSyncsPerRender);
136 }
137
138 } // namespace Adaptor
139
140 } // namespace Internal
141
142 } // namespace Dali