From caaa404d968743e22ecdffe210810b2d984fff18 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Fri, 14 Jan 2022 17:00:24 -0800 Subject: [PATCH] add basic support for Android Thermal Manager --- libs/utils/CMakeLists.txt | 3 + libs/utils/include/utils/ThermalManager.h | 26 ++++++++ .../include/utils/android/ThermalManager.h | 60 +++++++++++++++++++ .../include/utils/generic/ThermalManager.h | 56 +++++++++++++++++ libs/utils/src/android/ThermalManager.cpp | 55 +++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 libs/utils/include/utils/ThermalManager.h create mode 100644 libs/utils/include/utils/android/ThermalManager.h create mode 100644 libs/utils/include/utils/generic/ThermalManager.h create mode 100644 libs/utils/src/android/ThermalManager.cpp diff --git a/libs/utils/CMakeLists.txt b/libs/utils/CMakeLists.txt index 468dd51fdd..f2cb243566 100644 --- a/libs/utils/CMakeLists.txt +++ b/libs/utils/CMakeLists.txt @@ -73,6 +73,9 @@ set(SRCS if (WIN32) list(APPEND SRCS src/win32/Path.cpp) endif() +if (ANDROID) + list(APPEND SRCS src/android/ThermalManager.cpp) +endif() if (LINUX OR ANDROID) list(APPEND SRCS src/linux/Condition.cpp) list(APPEND SRCS src/linux/Mutex.cpp) diff --git a/libs/utils/include/utils/ThermalManager.h b/libs/utils/include/utils/ThermalManager.h new file mode 100644 index 0000000000..fc38d88edf --- /dev/null +++ b/libs/utils/include/utils/ThermalManager.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TNT_UTILS_THERMALMANAGER_H +#define TNT_UTILS_THERMALMANAGER_H + +#if defined(__ANDROID__) +#include +#else +#include +#endif + +#endif // TNT_UTILS_THERMALMANAGER_H diff --git a/libs/utils/include/utils/android/ThermalManager.h b/libs/utils/include/utils/android/ThermalManager.h new file mode 100644 index 0000000000..6c303b04bb --- /dev/null +++ b/libs/utils/include/utils/android/ThermalManager.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TNT_UTILS_ANDROID_THERMALMANAGER_H +#define TNT_UTILS_ANDROID_THERMALMANAGER_H + +#include + +#include + +struct AThermalManager; + +namespace utils { + +class ThermalManager { +public: + enum class ThermalStatus : int8_t { + ERROR = -1, + NONE, + LIGHT, + MODERATE, + SEVERE, + CRITICAL, + EMERGENCY, + SHUTDOWN + }; + + ThermalManager(); + ~ThermalManager(); + + // Movable + ThermalManager(ThermalManager&& rhs) noexcept; + ThermalManager& operator=(ThermalManager&& rhs) noexcept; + + // not copiable + ThermalManager(ThermalManager const& rhs) = delete; + ThermalManager& operator=(ThermalManager const& rhs) = delete; + + ThermalStatus getCurrentThermalStatus() const noexcept; + +private: + AThermalManager* mThermalManager = nullptr; +}; + +} // namespace utils + +#endif // TNT_UTILS_ANDROID_THERMALMANAGER_H diff --git a/libs/utils/include/utils/generic/ThermalManager.h b/libs/utils/include/utils/generic/ThermalManager.h new file mode 100644 index 0000000000..2d0088e561 --- /dev/null +++ b/libs/utils/include/utils/generic/ThermalManager.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TNT_UTILS_GENERIC_THERMALMANAGER_H +#define TNT_UTILS_GENERIC_THERMALMANAGER_H + +#include + +#include + +namespace utils { + +class ThermalManager { +public: + enum class ThermalStatus : int8_t { + ERROR = -1, + NONE, + LIGHT, + MODERATE, + SEVERE, + CRITICAL, + EMERGENCY, + SHUTDOWN + }; + + ThermalManager() = default; + + // Movable + ThermalManager(ThermalManager&& rhs) noexcept = default; + ThermalManager& operator=(ThermalManager&& rhs) noexcept = default; + + // not copiable + ThermalManager(ThermalManager const& rhs) = delete; + ThermalManager& operator=(ThermalManager const& rhs) = delete; + + ThermalStatus getCurrentThermalStatus() const noexcept { + return ThermalStatus::NONE; + } +}; + +} // namespace utils + +#endif // TNT_UTILS_GENERIC_THERMALMANAGER_H diff --git a/libs/utils/src/android/ThermalManager.cpp b/libs/utils/src/android/ThermalManager.cpp new file mode 100644 index 0000000000..6009abc927 --- /dev/null +++ b/libs/utils/src/android/ThermalManager.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include + +namespace utils { + +ThermalManager::ThermalManager() { + if (__builtin_available(android 30, *)) { + mThermalManager = AThermal_acquireManager(); + } +} + +ThermalManager::~ThermalManager() { + if (__builtin_available(android 30, *)) { + AThermal_releaseManager(mThermalManager); + } +} + +ThermalManager::ThermalManager(ThermalManager&& rhs) noexcept + : mThermalManager(rhs.mThermalManager) { + rhs.mThermalManager = nullptr; +} + +ThermalManager& ThermalManager::operator=(ThermalManager&& rhs) noexcept { + std::swap(mThermalManager, rhs.mThermalManager); + return *this; +} + +ThermalManager::ThermalStatus ThermalManager::getCurrentThermalStatus() const noexcept { + if (__builtin_available(android 30, *)) { + return (ThermalManager::ThermalStatus)AThermal_getCurrentThermalStatus(mThermalManager); + } else { + return ThermalStatus::NONE; + } +} + +} // namespace utils