• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Rockchip 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 #ifndef __MPP_TIME_H__
18 #define __MPP_TIME_H__
19 
20 #include "rk_type.h"
21 #include "mpp_thread.h"
22 
23 #if defined(_WIN32) && !defined(__MINGW32CE__)
24 #include <windows.h>
25 #define msleep                  Sleep
26 #define sleep(x)                Sleep((x)*1000)
27 #else
28 #include <unistd.h>
29 #define msleep(x)               usleep((x)*1000)
30 #endif
31 
32 typedef void* MppClock;
33 typedef void* MppTimer;
34 typedef void* MppStopwatch;
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 RK_S64 mpp_time();
41 void mpp_time_diff(RK_S64 start, RK_S64 end, RK_S64 limit, const char *fmt);
42 
43 /*
44  * Clock create / destroy / enable / disable function
45  * Note when clock is create it is default disabled user need to call enable
46  * fucntion with enable = 1 to enable the clock.
47  * User can use enable function with enable = 0 to disable the clock.
48  */
49 MppClock mpp_clock_get(const char *name);
50 void mpp_clock_put(MppClock clock);
51 void mpp_clock_enable(MppClock clock, RK_U32 enable);
52 
53 /*
54  * Clock basic operation function:
55  * start : let clock start timing counter
56  * pause : let clock pause and return the diff to start time
57  * reset : let clock counter to all zero
58  */
59 RK_S64 mpp_clock_start(MppClock clock);
60 RK_S64 mpp_clock_pause(MppClock clock);
61 RK_S64 mpp_clock_reset(MppClock clock);
62 
63 /*
64  * These clock helper function can only be call when clock is paused:
65  * mpp_clock_get_sum    - Return clock sum up total value
66  * mpp_clock_get_count  - Return clock sum up counter value
67  * mpp_clock_get_name   - Return clock name
68  */
69 RK_S64 mpp_clock_get_sum(MppClock clock);
70 RK_S64 mpp_clock_get_count(MppClock clock);
71 const char *mpp_clock_get_name(MppClock clock);
72 
73 /*
74  * MppTimer is for timer with callback function
75  * It will provide the ability to repeat doing something until it is
76  * disalble or put.
77  *
78  * Timer work flow:
79  *
80  * 1. mpp_timer_get
81  * 2. mpp_timer_set_callback
82  * 3. mpp_timer_set_timing(initial, interval)
83  * 4. mpp_timer_set_enable(initial, 1)
84  *    ... running ...
85  * 5. mpp_timer_set_enable(initial, 0)
86  * 6. mpp_timer_put
87  */
88 MppTimer mpp_timer_get(const char *name);
89 void mpp_timer_set_callback(MppTimer timer, MppThreadFunc func, void *ctx);
90 void mpp_timer_set_timing(MppTimer timer, RK_S32 initial, RK_S32 interval);
91 void mpp_timer_set_enable(MppTimer timer, RK_S32 enable);
92 void mpp_timer_put(MppTimer timer);
93 
94 /*
95  * MppStopwatch is for timer to record event and time
96  *
97  * Stopwatch work flow:
98  *
99  * 1. mpp_stopwatch_get
100  * 2. mpp_stopwatch_setup(max_count, show_on_exit)
101  * 3. mpp_stopwatch_record(event)
102  *    ... running ...
103  * 4. mpp_stopwatch_record(event)
104  * 5. mpp_stopwatch_put (show events and time)
105  */
106 MppStopwatch mpp_stopwatch_get(const char *name);
107 void mpp_stopwatch_set_show_on_exit(MppStopwatch stopwatch, RK_S32 show_on_exit);
108 void mpp_stopwatch_record(MppStopwatch stopwatch, const char *event);
109 void mpp_stopwatch_put(MppStopwatch timer);
110 RK_S64 mpp_stopwatch_elapsed_time(MppStopwatch stopwatch);
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #ifdef __cplusplus
117 class AutoTiming
118 {
119 public:
120     AutoTiming(const char *name = "AutoTiming");
121     ~AutoTiming();
122 private:
123     const char  *mName;
124     RK_S64      mStart;
125     RK_S64      mEnd;
126 
127     AutoTiming(const AutoTiming &);
128     AutoTiming &operator = (const AutoTiming&);
129 };
130 
131 #endif
132 
133 #define AUTO_TIMER_STRING(name, cnt)        name ## cnt
134 #define AUTO_TIMER_NAME_STRING(name, cnt)   AUTO_TIMER_STRING(name, cnt)
135 #define AUTO_TIMER_NAME(name)               AUTO_TIMER_NAME_STRING(name, __COUNTER__)
136 #define AUTO_TIMING()                       AutoTiming AUTO_TIMER_NAME(auto_timing)(__FUNCTION__)
137 
138 #endif /*__MPP_TIME_H__*/
139