1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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 __COMMON_CLOCK_H__
18 #define __COMMON_CLOCK_H__
19 
20 #include <stdint.h>
21 
22 #include <utils/Errors.h>
23 #include <utils/LinearTransform.h>
24 #include <utils/threads.h>
25 
26 namespace android {
27 
28 class CommonClock {
29   public:
30     CommonClock();
31 
32     bool      init(uint64_t local_freq);
33 
34     status_t  localToCommon(int64_t local, int64_t *common_out) const;
35     status_t  commonToLocal(int64_t common, int64_t *local_out) const;
36     int64_t   localDurationToCommonDuration(int64_t localDur) const;
getCommonFreq()37     uint64_t  getCommonFreq() const { return kCommonFreq; }
isValid()38     bool      isValid() const { return cur_trans_valid_; }
39     status_t  setSlew(int64_t change_time, int32_t ppm);
40     void      setBasis(int64_t local, int64_t common);
41     void      resetBasis();
42   private:
43     mutable Mutex lock_;
44 
45     int32_t  cur_slew_;
46     uint32_t local_to_common_freq_numer_;
47     uint32_t local_to_common_freq_denom_;
48 
49     LinearTransform duration_trans_;
50     LinearTransform cur_trans_;
51     bool cur_trans_valid_;
52 
53     static const uint64_t kCommonFreq = 1000000ull;
54 };
55 
56 }  // namespace android
57 #endif  // __COMMON_CLOCK_H__
58