e5bf6768e8e9eeb575815498ed114cd3ed9bd69d
[platform/core/uifw/dali-adaptor.git] / adaptors / base / single-threaded / single-thread-controller.h
1 #ifndef __DALI_INTERNAL_SINGLE_THREAD_CONTROLLER_H__
2 #define __DALI_INTERNAL_SINGLE_THREAD_CONTROLLER_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <stdint.h>
23 #include <dali/public-api/signals/connection-tracker.h>
24 #include <dali/integration-api/core.h>
25
26 // INTERNAL INCLUDES
27 #include <timer.h>
28 #include <base/interfaces/performance-interface.h>
29 #include <base/fps-tracker.h>
30 #include <base/render-helper.h>
31 #include <base/thread-controller-interface.h>
32 #include <base/update-status-logger.h>
33
34 namespace Dali
35 {
36
37 class RenderSurface;
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 class AdaptorInternalServices;
46 class EnvironmentOptions;
47
48 /**
49  * Single Thread Controller, where events, updates & renders ALL occur on the same thread.
50  */
51 class SingleThreadController : public ConnectionTracker,
52                                public ThreadControllerInterface
53 {
54 public:
55
56   /**
57    * Constructor
58    */
59   SingleThreadController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions );
60
61   /**
62    * Non virtual destructor. Not intended as base class.
63    */
64   ~SingleThreadController();
65
66   /**
67    * @copydoc ThreadControllerInterface::Initialize()
68    */
69   void Initialize();
70
71   /**
72    * @copydoc ThreadControllerInterface::Start()
73    */
74   void Start();
75
76   /**
77    * @copydoc ThreadControllerInterface::Pause()
78    */
79   void Pause();
80
81   /**
82    * @copydoc ThreadControllerInterface::Resume()
83    */
84   void Resume();
85
86   /**
87    * @copydoc ThreadControllerInterface::Stop()
88    */
89   void Stop();
90
91   /**
92    * @copydoc ThreadControllerInterface::RequestUpdate()
93    */
94   void RequestUpdate();
95
96   /**
97    * @copydoc ThreadControllerInterface::RequestUpdateOnce()
98    */
99   void RequestUpdateOnce();
100
101   /**
102    * @copydoc ThreadControllerInterface::ReplaceSurface()
103    */
104   void ReplaceSurface( RenderSurface* surface );
105
106   /**
107    * @copydoc ThreadControllerInterface::SetRenderRefreshRate()
108    */
109   void SetRenderRefreshRate( unsigned int refreshRate );
110
111 private:
112
113   /**
114    * State Machine
115    */
116   struct State
117   {
118     enum Type
119     {
120       STOPPED,
121       RUNNING,
122       PAUSED,
123       SLEEPING
124     };
125   };
126
127   // Undefined copy constructor.
128   SingleThreadController( const SingleThreadController& );
129
130   // Undefined assignment operator.
131   SingleThreadController& operator=( const SingleThreadController& );
132
133   /**
134    * Ticks whenever the timer expires
135    */
136   bool OnTimerTick();
137
138   /**
139    * Runs the update and render
140    *
141    * @param[in] incrementTime If true, then the animation times are incremented.
142    */
143   void UpdateRender( bool incrementTime );
144
145   /**
146    * Updates mCurrentTime and gets the time elapsed (in seconds) since last time this function was called.
147    *
148    * @return time elapsed (in seconds) since last call.
149    */
150   float UpdateTimeSinceLastRender();
151
152   /**
153    * Helper to add a performance marker to the performance server (if it's active)
154    * @param type performance marker type
155    */
156   void AddPerformanceMarker( PerformanceInterface::MarkerType type );
157
158   /**
159    * Changes the state and performs any other state-change related functionality.
160    * @param[in] state The new state
161    */
162   void ChangeState( State::Type state );
163
164   /**
165    * Performs operations to stop rendering, e.g. informing Core of context being destroyed & shutting down EGL.
166    */
167   void StopRendering();
168
169 private:
170
171   Dali::Timer                       mTimer;                           ///< Ensures an update & render is run every frame.
172   FpsTracker                        mFpsTracker;                      ///< Object that tracks the FPS
173   UpdateStatusLogger                mUpdateStatusLogger;              ///< Object that logs the update-status as required.
174
175   RenderHelper                      mRenderHelper;                    ///< Helper class for EGL, pre & post rendering
176
177   Integration::Core&                mCore;                            ///< DALi core reference
178   PerformanceInterface*             mPerformanceInterface;            ///< The performance logging interface
179
180   uint64_t                          mLastUpdateRenderTime;            ///< Last time we did an update and render
181   uint64_t                          mSystemTime;                      ///< The current system time for FPS calculations
182   unsigned int                      mRefreshRate;                     ///< Frame skipping count
183   State::Type                       mState;                           ///< The state
184   bool                              mUpdatingAndRendering:1;          ///< Set to true when we are updating and rendering.
185   bool                              mStopRequestedWhileRendering:1;   ///< Set to true if we were told to stop while we were in the middle of a render
186 };
187
188 } // namespace Adaptor
189
190 } // namespace Internal
191
192 } // namespace Dali
193
194 #endif // __DALI_INTERNAL_SINGLE_THREAD_CONTROLLER_H__