2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define __STDC_LIMIT_MACROS
19 #define LOG_TAG "common_time"
20 #include <utils/Log.h>
25 #include <utils/Errors.h>
26 #include <utils/LinearTransform.h>
28 #include "common_clock.h"
32 CommonClock::CommonClock() {
34 cur_trans_valid_ = false;
36 cur_trans_.a_zero = 0;
37 cur_trans_.b_zero = 0;
38 cur_trans_.a_to_b_numer = local_to_common_freq_numer_ = 1;
39 cur_trans_.a_to_b_denom = local_to_common_freq_denom_ = 1;
40 duration_trans_ = cur_trans_;
43 bool CommonClock::init(uint64_t local_freq) {
44 Mutex::Autolock lock(&lock_);
49 uint64_t numer = kCommonFreq;
50 uint64_t denom = local_freq;
52 LinearTransform::reduce(&numer, &denom);
53 if ((numer > UINT32_MAX) || (denom > UINT32_MAX)) {
54 ALOGE("Overflow in CommonClock::init while trying to reduce %" PRIu64 "/%" PRIu64,
55 kCommonFreq, local_freq);
59 cur_trans_.a_to_b_numer = local_to_common_freq_numer_ =
60 static_cast<uint32_t>(numer);
61 cur_trans_.a_to_b_denom = local_to_common_freq_denom_ =
62 static_cast<uint32_t>(denom);
63 duration_trans_ = cur_trans_;
68 status_t CommonClock::localToCommon(int64_t local, int64_t *common_out) const {
69 Mutex::Autolock lock(&lock_);
71 if (!cur_trans_valid_)
72 return INVALID_OPERATION;
74 if (!cur_trans_.doForwardTransform(local, common_out))
75 return INVALID_OPERATION;
80 status_t CommonClock::commonToLocal(int64_t common, int64_t *local_out) const {
81 Mutex::Autolock lock(&lock_);
83 if (!cur_trans_valid_)
84 return INVALID_OPERATION;
86 if (!cur_trans_.doReverseTransform(common, local_out))
87 return INVALID_OPERATION;
92 int64_t CommonClock::localDurationToCommonDuration(int64_t localDur) const {
94 duration_trans_.doForwardTransform(localDur, &ret);
98 void CommonClock::setBasis(int64_t local, int64_t common) {
99 Mutex::Autolock lock(&lock_);
101 cur_trans_.a_zero = local;
102 cur_trans_.b_zero = common;
103 cur_trans_valid_ = true;
106 void CommonClock::resetBasis() {
107 Mutex::Autolock lock(&lock_);
109 cur_trans_.a_zero = 0;
110 cur_trans_.b_zero = 0;
111 cur_trans_valid_ = false;
114 status_t CommonClock::setSlew(int64_t change_time, int32_t ppm) {
115 Mutex::Autolock lock(&lock_);
117 int64_t new_local_basis;
118 int64_t new_common_basis;
120 if (cur_trans_valid_) {
121 new_local_basis = change_time;
122 if (!cur_trans_.doForwardTransform(change_time, &new_common_basis)) {
123 ALOGE("Overflow when attempting to set slew rate to %d", ppm);
124 return INVALID_OPERATION;
128 new_common_basis = 0;
132 uint32_t n1 = local_to_common_freq_numer_;
133 uint32_t n2 = 1000000 + cur_slew_;
135 uint32_t d1 = local_to_common_freq_denom_;
136 uint32_t d2 = 1000000;
138 // n1/d1 has already been reduced, no need to do so here.
139 LinearTransform::reduce(&n1, &d2);
140 LinearTransform::reduce(&n2, &d1);
141 LinearTransform::reduce(&n2, &d2);
143 cur_trans_.a_zero = new_local_basis;
144 cur_trans_.b_zero = new_common_basis;
145 cur_trans_.a_to_b_numer = n1 * n2;
146 cur_trans_.a_to_b_denom = d1 * d2;
151 } // namespace android