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