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