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