Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #include "HintSessionWrapper.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
Matt Buckley | 61726a3 | 2022-12-06 23:44:45 +0000 | [diff] [blame] | 20 | #include <private/performance_hint_private.h> |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 23 | #include <chrono> |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | #include "../Properties.h" |
| 27 | #include "thread/CommonPool.h" |
| 28 | |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 29 | using namespace std::chrono_literals; |
| 30 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | namespace renderthread { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | typedef APerformanceHintManager* (*APH_getManager)(); |
| 38 | typedef APerformanceHintSession* (*APH_createSession)(APerformanceHintManager*, const int32_t*, |
| 39 | size_t, int64_t); |
| 40 | typedef void (*APH_closeSession)(APerformanceHintSession* session); |
| 41 | typedef void (*APH_updateTargetWorkDuration)(APerformanceHintSession*, int64_t); |
| 42 | typedef void (*APH_reportActualWorkDuration)(APerformanceHintSession*, int64_t); |
| 43 | typedef void (*APH_sendHint)(APerformanceHintSession* session, int32_t); |
| 44 | |
| 45 | bool gAPerformanceHintBindingInitialized = false; |
| 46 | APH_getManager gAPH_getManagerFn = nullptr; |
| 47 | APH_createSession gAPH_createSessionFn = nullptr; |
| 48 | APH_closeSession gAPH_closeSessionFn = nullptr; |
| 49 | APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr; |
| 50 | APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr; |
| 51 | APH_sendHint gAPH_sendHintFn = nullptr; |
| 52 | |
| 53 | void ensureAPerformanceHintBindingInitialized() { |
| 54 | if (gAPerformanceHintBindingInitialized) return; |
| 55 | |
| 56 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
| 57 | LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!"); |
| 58 | |
| 59 | gAPH_getManagerFn = (APH_getManager)dlsym(handle_, "APerformanceHint_getManager"); |
| 60 | LOG_ALWAYS_FATAL_IF(gAPH_getManagerFn == nullptr, |
| 61 | "Failed to find required symbol APerformanceHint_getManager!"); |
| 62 | |
| 63 | gAPH_createSessionFn = (APH_createSession)dlsym(handle_, "APerformanceHint_createSession"); |
| 64 | LOG_ALWAYS_FATAL_IF(gAPH_createSessionFn == nullptr, |
| 65 | "Failed to find required symbol APerformanceHint_createSession!"); |
| 66 | |
| 67 | gAPH_closeSessionFn = (APH_closeSession)dlsym(handle_, "APerformanceHint_closeSession"); |
| 68 | LOG_ALWAYS_FATAL_IF(gAPH_closeSessionFn == nullptr, |
| 69 | "Failed to find required symbol APerformanceHint_closeSession!"); |
| 70 | |
| 71 | gAPH_updateTargetWorkDurationFn = (APH_updateTargetWorkDuration)dlsym( |
| 72 | handle_, "APerformanceHint_updateTargetWorkDuration"); |
| 73 | LOG_ALWAYS_FATAL_IF( |
| 74 | gAPH_updateTargetWorkDurationFn == nullptr, |
| 75 | "Failed to find required symbol APerformanceHint_updateTargetWorkDuration!"); |
| 76 | |
| 77 | gAPH_reportActualWorkDurationFn = (APH_reportActualWorkDuration)dlsym( |
| 78 | handle_, "APerformanceHint_reportActualWorkDuration"); |
| 79 | LOG_ALWAYS_FATAL_IF( |
| 80 | gAPH_reportActualWorkDurationFn == nullptr, |
| 81 | "Failed to find required symbol APerformanceHint_reportActualWorkDuration!"); |
| 82 | |
| 83 | gAPH_sendHintFn = (APH_sendHint)dlsym(handle_, "APerformanceHint_sendHint"); |
| 84 | LOG_ALWAYS_FATAL_IF(gAPH_sendHintFn == nullptr, |
| 85 | "Failed to find required symbol APerformanceHint_sendHint!"); |
| 86 | |
| 87 | gAPerformanceHintBindingInitialized = true; |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | HintSessionWrapper::HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId) |
| 93 | : mUiThreadId(uiThreadId), mRenderThreadId(renderThreadId) {} |
| 94 | |
| 95 | HintSessionWrapper::~HintSessionWrapper() { |
| 96 | if (mHintSession) { |
| 97 | gAPH_closeSessionFn(mHintSession); |
| 98 | } |
| 99 | } |
| 100 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 101 | bool HintSessionWrapper::init() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 102 | if (mHintSession != nullptr) return true; |
| 103 | |
| 104 | // If we're waiting for the session |
| 105 | if (mHintSessionFuture.valid()) { |
| 106 | // If the session is here |
| 107 | if (mHintSessionFuture.wait_for(0s) == std::future_status::ready) { |
| 108 | mHintSession = mHintSessionFuture.get(); |
| 109 | if (mHintSession != nullptr) { |
| 110 | mSessionValid = true; |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // If it broke last time we tried this, shouldn't be running, or |
Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 118 | // has bad argument values, don't even bother |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 119 | if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() || |
| 120 | mUiThreadId < 0 || mRenderThreadId < 0) { |
Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 121 | return false; |
| 122 | } |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 123 | |
| 124 | // Assume that if we return before the end, it broke |
| 125 | mSessionValid = false; |
| 126 | |
| 127 | ensureAPerformanceHintBindingInitialized(); |
| 128 | |
| 129 | APerformanceHintManager* manager = gAPH_getManagerFn(); |
| 130 | if (!manager) return false; |
| 131 | |
| 132 | std::vector<pid_t> tids = CommonPool::getThreadIds(); |
| 133 | tids.push_back(mUiThreadId); |
| 134 | tids.push_back(mRenderThreadId); |
| 135 | |
| 136 | // Use a placeholder target value to initialize, |
| 137 | // this will always be replaced elsewhere before it gets used |
| 138 | int64_t defaultTargetDurationNanos = 16666667; |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 139 | mHintSessionFuture = CommonPool::async([=, tids = std::move(tids)] { |
| 140 | return gAPH_createSessionFn(manager, tids.data(), tids.size(), defaultTargetDurationNanos); |
| 141 | }); |
| 142 | return false; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 146 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 147 | targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100; |
| 148 | if (targetWorkDurationNanos != mLastTargetWorkDuration && |
| 149 | targetWorkDurationNanos > kSanityCheckLowerBound && |
| 150 | targetWorkDurationNanos < kSanityCheckUpperBound) { |
| 151 | mLastTargetWorkDuration = targetWorkDurationNanos; |
| 152 | gAPH_updateTargetWorkDurationFn(mHintSession, targetWorkDurationNanos); |
| 153 | } |
| 154 | mLastFrameNotification = systemTime(); |
| 155 | } |
| 156 | |
| 157 | void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 158 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 159 | if (actualDurationNanos > kSanityCheckLowerBound && |
| 160 | actualDurationNanos < kSanityCheckUpperBound) { |
| 161 | gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void HintSessionWrapper::sendLoadResetHint() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 166 | if (!init()) return; |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 167 | nsecs_t now = systemTime(); |
| 168 | if (now - mLastFrameNotification > kResetHintTimeout) { |
| 169 | gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_RESET)); |
| 170 | } |
| 171 | mLastFrameNotification = now; |
| 172 | } |
| 173 | |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 174 | void HintSessionWrapper::sendLoadIncreaseHint() { |
Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame^] | 175 | if (!init()) return; |
Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 176 | gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_UP)); |
| 177 | } |
| 178 | |
Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 179 | } /* namespace renderthread */ |
| 180 | } /* namespace uirenderer */ |
| 181 | } /* namespace android */ |