media/omx-plugin/include/ics/utils/Timers.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/omx-plugin/include/ics/utils/Timers.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/*
     1.5 + * Copyright (C) 2005 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +//
    1.21 +// Timer functions.
    1.22 +//
    1.23 +#ifndef _LIBS_UTILS_TIMERS_H
    1.24 +#define _LIBS_UTILS_TIMERS_H
    1.25 +
    1.26 +#include <stdint.h>
    1.27 +#include <sys/types.h>
    1.28 +#include <sys/time.h>
    1.29 +
    1.30 +// ------------------------------------------------------------------
    1.31 +// C API
    1.32 +
    1.33 +#ifdef __cplusplus
    1.34 +extern "C" {
    1.35 +#endif
    1.36 +
    1.37 +typedef int64_t nsecs_t;       // nano-seconds
    1.38 +
    1.39 +static inline nsecs_t seconds_to_nanoseconds(nsecs_t secs)
    1.40 +{
    1.41 +    return secs*1000000000;
    1.42 +}
    1.43 +
    1.44 +static inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs)
    1.45 +{
    1.46 +    return secs*1000000;
    1.47 +}
    1.48 +
    1.49 +static inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs)
    1.50 +{
    1.51 +    return secs*1000;
    1.52 +}
    1.53 +
    1.54 +static inline nsecs_t nanoseconds_to_seconds(nsecs_t secs)
    1.55 +{
    1.56 +    return secs/1000000000;
    1.57 +}
    1.58 +
    1.59 +static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
    1.60 +{
    1.61 +    return secs/1000000;
    1.62 +}
    1.63 +
    1.64 +static inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs)
    1.65 +{
    1.66 +    return secs/1000;
    1.67 +}
    1.68 +
    1.69 +static inline nsecs_t s2ns(nsecs_t v)  {return seconds_to_nanoseconds(v);}
    1.70 +static inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);}
    1.71 +static inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);}
    1.72 +static inline nsecs_t ns2s(nsecs_t v)  {return nanoseconds_to_seconds(v);}
    1.73 +static inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);}
    1.74 +static inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);}
    1.75 +
    1.76 +static inline nsecs_t seconds(nsecs_t v)      { return s2ns(v); }
    1.77 +static inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); }
    1.78 +static inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); }
    1.79 +
    1.80 +enum {
    1.81 +    SYSTEM_TIME_REALTIME = 0,  // system-wide realtime clock
    1.82 +    SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
    1.83 +    SYSTEM_TIME_PROCESS = 2,   // high-resolution per-process clock
    1.84 +    SYSTEM_TIME_THREAD = 3     // high-resolution per-thread clock
    1.85 +};
    1.86 +    
    1.87 +// return the system-time according to the specified clock
    1.88 +#ifdef __cplusplus
    1.89 +nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC);
    1.90 +#else
    1.91 +nsecs_t systemTime(int clock);
    1.92 +#endif // def __cplusplus
    1.93 +
    1.94 +/**
    1.95 + * Returns the number of milliseconds to wait between the reference time and the timeout time.
    1.96 + * If the timeout is in the past relative to the reference time, returns 0.
    1.97 + * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time,
    1.98 + * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay.
    1.99 + * Otherwise, returns the difference between the reference time and timeout time
   1.100 + * rounded up to the next millisecond.
   1.101 + */
   1.102 +int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime);
   1.103 +
   1.104 +#ifdef __cplusplus
   1.105 +} // extern "C"
   1.106 +#endif
   1.107 +
   1.108 +// ------------------------------------------------------------------
   1.109 +// C++ API
   1.110 +
   1.111 +#ifdef __cplusplus
   1.112 +
   1.113 +namespace android {
   1.114 +/*
   1.115 + * Time the duration of something.
   1.116 + *
   1.117 + * Includes some timeval manipulation functions.
   1.118 + */
   1.119 +class DurationTimer {
   1.120 +public:
   1.121 +    DurationTimer() {}
   1.122 +    ~DurationTimer() {}
   1.123 +
   1.124 +    // Start the timer.
   1.125 +    void start();
   1.126 +    // Stop the timer.
   1.127 +    void stop();
   1.128 +    // Get the duration in microseconds.
   1.129 +    long long durationUsecs() const;
   1.130 +
   1.131 +    // Subtract two timevals.  Returns the difference (ptv1-ptv2) in
   1.132 +    // microseconds.
   1.133 +    static long long subtractTimevals(const struct timeval* ptv1,
   1.134 +        const struct timeval* ptv2);
   1.135 +
   1.136 +    // Add the specified amount of time to the timeval.
   1.137 +    static void addToTimeval(struct timeval* ptv, long usec);
   1.138 +
   1.139 +private:
   1.140 +    struct timeval  mStartWhen;
   1.141 +    struct timeval  mStopWhen;
   1.142 +};
   1.143 +
   1.144 +}; // android
   1.145 +#endif // def __cplusplus
   1.146 +
   1.147 +#endif // _LIBS_UTILS_TIMERS_H

mercurial