Chong Zhang | 8677f1f | 2021-01-21 20:37:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "TranscodingThermalPolicy" |
| 19 | |
| 20 | #include <media/TranscodingThermalPolicy.h> |
| 21 | #include <media/TranscodingUidPolicy.h> |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | static bool needThrottling(AThermalStatus status) { |
| 27 | return (status >= ATHERMAL_STATUS_SEVERE); |
| 28 | } |
| 29 | |
| 30 | //static |
| 31 | void TranscodingThermalPolicy::onStatusChange(void* data, AThermalStatus status) { |
| 32 | TranscodingThermalPolicy* policy = static_cast<TranscodingThermalPolicy*>(data); |
| 33 | policy->onStatusChange(status); |
| 34 | } |
| 35 | |
| 36 | TranscodingThermalPolicy::TranscodingThermalPolicy() |
| 37 | : mRegistered(false), mThermalManager(nullptr), mIsThrottling(false) { |
| 38 | registerSelf(); |
| 39 | } |
| 40 | |
| 41 | TranscodingThermalPolicy::~TranscodingThermalPolicy() { |
| 42 | unregisterSelf(); |
| 43 | } |
| 44 | |
| 45 | void TranscodingThermalPolicy::registerSelf() { |
| 46 | ALOGI("TranscodingThermalPolicy: registerSelf"); |
| 47 | |
| 48 | std::scoped_lock lock{mRegisteredLock}; |
| 49 | |
| 50 | if (mRegistered) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (__builtin_available(android __TRANSCODING_MIN_API__, *)) { |
| 55 | AThermalManager* thermalManager = AThermal_acquireManager(); |
| 56 | if (thermalManager == nullptr) { |
| 57 | ALOGE("Failed to acquire thermal manager"); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | int ret = AThermal_registerThermalStatusListener(thermalManager, onStatusChange, this); |
| 62 | if (ret != 0) { |
| 63 | ALOGE("Failed to register thermal status listener"); |
| 64 | AThermal_releaseManager(thermalManager); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | mIsThrottling = needThrottling(AThermal_getCurrentThermalStatus(thermalManager)); |
| 69 | mThermalManager = thermalManager; |
| 70 | } |
| 71 | |
| 72 | mRegistered = true; |
| 73 | } |
| 74 | |
| 75 | void TranscodingThermalPolicy::unregisterSelf() { |
| 76 | ALOGI("TranscodingThermalPolicy: unregisterSelf"); |
| 77 | |
| 78 | std::scoped_lock lock{mRegisteredLock}; |
| 79 | |
| 80 | if (!mRegistered) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if (__builtin_available(android __TRANSCODING_MIN_API__, *)) { |
| 85 | if (mThermalManager != nullptr) { |
| 86 | // Unregister listener |
| 87 | int ret = |
| 88 | AThermal_unregisterThermalStatusListener(mThermalManager, onStatusChange, this); |
| 89 | if (ret != 0) { |
| 90 | ALOGW("Failed to unregister thermal status listener"); |
| 91 | } |
| 92 | AThermal_releaseManager(mThermalManager); |
| 93 | mThermalManager = nullptr; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | mRegistered = false; |
| 98 | } |
| 99 | |
| 100 | void TranscodingThermalPolicy::setCallback( |
| 101 | const std::shared_ptr<ThermalPolicyCallbackInterface>& cb) { |
| 102 | std::scoped_lock lock{mCallbackLock}; |
| 103 | mThermalPolicyCallback = cb; |
| 104 | } |
| 105 | |
| 106 | bool TranscodingThermalPolicy::getThrottlingStatus() { |
| 107 | std::scoped_lock lock{mRegisteredLock}; |
| 108 | return mIsThrottling; |
| 109 | } |
| 110 | |
| 111 | void TranscodingThermalPolicy::onStatusChange(AThermalStatus status) { |
| 112 | bool isThrottling = needThrottling(status); |
| 113 | |
| 114 | { |
| 115 | std::scoped_lock lock{mRegisteredLock}; |
| 116 | if (isThrottling == mIsThrottling) { |
| 117 | return; |
| 118 | } |
| 119 | ALOGI("Transcoding thermal throttling changed: %d", isThrottling); |
| 120 | mIsThrottling = isThrottling; |
| 121 | } |
| 122 | |
| 123 | std::scoped_lock lock{mCallbackLock}; |
| 124 | std::shared_ptr<ThermalPolicyCallbackInterface> cb; |
| 125 | if ((cb = mThermalPolicyCallback.lock()) != nullptr) { |
| 126 | if (isThrottling) { |
| 127 | cb->onThrottlingStarted(); |
| 128 | } else { |
| 129 | cb->onThrottlingStopped(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } // namespace android |