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 #define __STDC_LIMIT_MACROS 18 19 #define LOG_TAG "common_time" 20 #include <utils/Log.h> 21 22 #include <stdint.h> 23 24 #include <utils/Errors.h> 25 #include <utils/LinearTransform.h> 26 27 #include "common_clock.h" 28 29 namespace android { 30 31 CommonClock::CommonClock() { 32 cur_slew_ = 0; 33 cur_trans_valid_ = false; 34 35 cur_trans_.a_zero = 0; 36 cur_trans_.b_zero = 0; 37 cur_trans_.a_to_b_numer = local_to_common_freq_numer_ = 1; 38 cur_trans_.a_to_b_denom = local_to_common_freq_denom_ = 1; 39 duration_trans_ = cur_trans_; 40 } 41 42 bool CommonClock::init(uint64_t local_freq) { 43 Mutex::Autolock lock(&lock_); 44 45 if (!local_freq) 46 return false; 47 48 uint64_t numer = kCommonFreq; 49 uint64_t denom = local_freq; 50 51 LinearTransform::reduce(&numer, &denom); 52 if ((numer > UINT32_MAX) || (denom > UINT32_MAX)) { 53 ALOGE("Overflow in CommonClock::init while trying to reduce %lld/%lld", 54 kCommonFreq, local_freq); 55 return false; 56 } 57 58 cur_trans_.a_to_b_numer = local_to_common_freq_numer_ = 59 static_cast<uint32_t>(numer); 60 cur_trans_.a_to_b_denom = local_to_common_freq_denom_ = 61 static_cast<uint32_t>(denom); 62 duration_trans_ = cur_trans_; 63 64 return true; 65 } 66 67 status_t CommonClock::localToCommon(int64_t local, int64_t *common_out) const { 68 Mutex::Autolock lock(&lock_); 69 70 if (!cur_trans_valid_) 71 return INVALID_OPERATION; 72 73 if (!cur_trans_.doForwardTransform(local, common_out)) 74 return INVALID_OPERATION; 75 76 return OK; 77 } 78 79 status_t CommonClock::commonToLocal(int64_t common, int64_t *local_out) const { 80 Mutex::Autolock lock(&lock_); 81 82 if (!cur_trans_valid_) 83 return INVALID_OPERATION; 84 85 if (!cur_trans_.doReverseTransform(common, local_out)) 86 return INVALID_OPERATION; 87 88 return OK; 89 } 90 91 int64_t CommonClock::localDurationToCommonDuration(int64_t localDur) const { 92 int64_t ret; 93 duration_trans_.doForwardTransform(localDur, &ret); 94 return ret; 95 } 96 97 void CommonClock::setBasis(int64_t local, int64_t common) { 98 Mutex::Autolock lock(&lock_); 99 100 cur_trans_.a_zero = local; 101 cur_trans_.b_zero = common; 102 cur_trans_valid_ = true; 103 } 104 105 void CommonClock::resetBasis() { 106 Mutex::Autolock lock(&lock_); 107 108 cur_trans_.a_zero = 0; 109 cur_trans_.b_zero = 0; 110 cur_trans_valid_ = false; 111 } 112 113 status_t CommonClock::setSlew(int64_t change_time, int32_t ppm) { 114 Mutex::Autolock lock(&lock_); 115 116 int64_t new_local_basis; 117 int64_t new_common_basis; 118 119 if (cur_trans_valid_) { 120 new_local_basis = change_time; 121 if (!cur_trans_.doForwardTransform(change_time, &new_common_basis)) { 122 ALOGE("Overflow when attempting to set slew rate to %d", ppm); 123 return INVALID_OPERATION; 124 } 125 } else { 126 new_local_basis = 0; 127 new_common_basis = 0; 128 } 129 130 cur_slew_ = ppm; 131 uint32_t n1 = local_to_common_freq_numer_; 132 uint32_t n2 = 1000000 + cur_slew_; 133 134 uint32_t d1 = local_to_common_freq_denom_; 135 uint32_t d2 = 1000000; 136 137 // n1/d1 has already been reduced, no need to do so here. 138 LinearTransform::reduce(&n1, &d2); 139 LinearTransform::reduce(&n2, &d1); 140 LinearTransform::reduce(&n2, &d2); 141 142 cur_trans_.a_zero = new_local_basis; 143 cur_trans_.b_zero = new_common_basis; 144 cur_trans_.a_to_b_numer = n1 * n2; 145 cur_trans_.a_to_b_denom = d1 * d2; 146 147 return OK; 148 } 149 150 } // namespace android 151