Improve ThreadSynchronization Logging (enable release logging if required)
[platform/core/uifw/dali-adaptor.git] / adaptors / base / frame-time.h
1 #ifndef __DALI_INTERNAL_ADAPTOR_FRAME_TIME_H__
2 #define __DALI_INTERNAL_ADAPTOR_FRAME_TIME_H__
3
4 /*
5  * Copyright (c) 2014 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
24 namespace Dali
25 {
26
27 namespace Integration
28 {
29 class PlatformAbstraction;
30 }
31
32 namespace Internal
33 {
34 namespace Adaptor
35 {
36
37 /**
38  * FrameTime stores the time of the last VSync. It can then be used by the update thread to predict
39  * the current update will be rendered.
40  */
41 class FrameTime
42 {
43 public:
44
45   // Called from Event thread
46
47   /**
48    * Constructor
49    * @param[in]  platform  The platform used to retrieve the current time
50    */
51   FrameTime( Integration::PlatformAbstraction& platform );
52
53   /**
54    * Destructor, non virtual
55    */
56   ~FrameTime();
57
58   /**
59    * Sets the expected minimum frame time interval.
60    * @param[in]  interval  The interval in microseconds.
61    */
62   void SetMinimumFrameTimeInterval( unsigned int interval );
63
64   /**
65    * Suspends the FrameTime object when the application state changes
66    */
67   void Suspend();
68
69   /**
70    * Resumes the FrameTime object when the application state changes
71    */
72   void Resume();
73
74   // Called from Update thread
75
76   /**
77    * Sets the FrameTime object to sleep, i.e. when there are no more updates required.
78    */
79   void Sleep();
80
81   /**
82    * Wakes the FrameTime object from a sleep state.
83    */
84   void WakeUp();
85
86   /**
87    * Predicts when the next render time will occur.
88    *
89    * @param[out]  lastFrameDeltaSeconds      The delta, in seconds (with float precision), between the last two renders.
90    * @param[out]  lastSyncTimeMilliseconds  The time, in milliseconds, of the last Sync.
91    * @param[out]  nextSyncTimeMilliseconds  The estimated time, in milliseconds, at the next Sync.
92    *
93    * @note Should only be called once per tick, from the update thread.
94    */
95   void PredictNextSyncTime( float& lastFrameDeltaSeconds, unsigned int& lastVSyncTimeMilliseconds, unsigned int& nextVSyncTimeMilliseconds );
96
97   // Called from VSync thread
98
99   /**
100    * Tells the FrameTime object that a Sync has occurred.
101    *
102    * @param[in]  frameNumber  The frame number of the current Sync.
103    *
104    * @note Should only be called from the VSync thread.
105    */
106   void SetSyncTime( unsigned int frameNumber );
107
108 private:
109
110   /**
111    * Sets the current time to be the last Vsync time.
112    */
113   inline void SetLastSyncTime();
114
115 private:
116
117   Integration::PlatformAbstraction& mPlatform; ///< The platform abstraction.
118
119   unsigned int mMinimumFrameTimeInterval; ///< The minimum frame time interval, set by Adaptor.
120
121   uint64_t mLastSyncTime;                ///< The last Sync time (in microseconds).
122   uint64_t mLastSyncTimeAtUpdate;        ///< The last Sync time at Update (in microseconds).
123
124   unsigned int mLastSyncFrameNumber;     ///< The last Sync frame number
125   unsigned int mLastUpdateFrameNumber;   ///< The last Sync frame number handled in Update.
126
127   // NOTE cannot use bitfields or booleans as these are used from multiple threads, must use variable with machine word size for atomic read/write
128   unsigned int mRunning;                 ///< The state of the FrameTime object.
129   unsigned int mFirstFrame;              ///< Whether the current update is the first frame (after initialisation, resume or wake up).
130
131   unsigned int mPreviousUpdateFrames[3]; ///< Array holding the number of frames Update took in the last three iterations.
132   unsigned int writePos;                 ///< The current write position in the array.
133
134   unsigned int mExtraUpdatesSinceSync;   ///< The number of extra updates since the last Sync.
135 };
136
137 } // namespace Adaptor
138 } // namespace Internal
139 } // namespace Dali
140
141 #endif // __DALI_INTERNAL_ADAPTOR_FRAME_TIME_H__