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