blob: 1f338ee523eb1325ffc1f42a529bed86ee27cc3d [file] [log] [blame]
Matt Buckleye9023cf2022-11-23 22:39:25 +00001/*
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 Buckley61726a32022-12-06 23:44:45 +000020#include <private/performance_hint_private.h>
Matt Buckleye9023cf2022-11-23 22:39:25 +000021#include <utils/Log.h>
22
Matt Buckley191f5cc2023-03-30 20:58:22 +000023#include <chrono>
Matt Buckleye9023cf2022-11-23 22:39:25 +000024#include <vector>
25
26#include "../Properties.h"
27#include "thread/CommonPool.h"
28
Matt Buckley191f5cc2023-03-30 20:58:22 +000029using namespace std::chrono_literals;
30
Matt Buckleye9023cf2022-11-23 22:39:25 +000031namespace android {
32namespace uirenderer {
33namespace renderthread {
34
35namespace {
36
37typedef APerformanceHintManager* (*APH_getManager)();
38typedef APerformanceHintSession* (*APH_createSession)(APerformanceHintManager*, const int32_t*,
39 size_t, int64_t);
40typedef void (*APH_closeSession)(APerformanceHintSession* session);
41typedef void (*APH_updateTargetWorkDuration)(APerformanceHintSession*, int64_t);
42typedef void (*APH_reportActualWorkDuration)(APerformanceHintSession*, int64_t);
43typedef void (*APH_sendHint)(APerformanceHintSession* session, int32_t);
44
45bool gAPerformanceHintBindingInitialized = false;
46APH_getManager gAPH_getManagerFn = nullptr;
47APH_createSession gAPH_createSessionFn = nullptr;
48APH_closeSession gAPH_closeSessionFn = nullptr;
49APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr;
50APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr;
51APH_sendHint gAPH_sendHintFn = nullptr;
52
53void 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
92HintSessionWrapper::HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId)
93 : mUiThreadId(uiThreadId), mRenderThreadId(renderThreadId) {}
94
95HintSessionWrapper::~HintSessionWrapper() {
Matt Buckley10d75fc2023-08-24 15:56:46 +000096 destroy();
97}
98
99void HintSessionWrapper::destroy() {
100 if (mHintSessionFuture.valid()) {
101 mHintSession = mHintSessionFuture.get();
102 }
Matt Buckleye9023cf2022-11-23 22:39:25 +0000103 if (mHintSession) {
104 gAPH_closeSessionFn(mHintSession);
Matt Buckley10d75fc2023-08-24 15:56:46 +0000105 mSessionValid = true;
106 mHintSession = nullptr;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000107 }
108}
109
Matt Buckleye9023cf2022-11-23 22:39:25 +0000110bool HintSessionWrapper::init() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000111 if (mHintSession != nullptr) return true;
112
113 // If we're waiting for the session
114 if (mHintSessionFuture.valid()) {
115 // If the session is here
116 if (mHintSessionFuture.wait_for(0s) == std::future_status::ready) {
117 mHintSession = mHintSessionFuture.get();
118 if (mHintSession != nullptr) {
119 mSessionValid = true;
120 return true;
121 }
122 }
123 return false;
124 }
125
126 // If it broke last time we tried this, shouldn't be running, or
Matt Buckley124d0c672023-01-19 03:04:19 +0000127 // has bad argument values, don't even bother
Matt Buckley191f5cc2023-03-30 20:58:22 +0000128 if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() ||
129 mUiThreadId < 0 || mRenderThreadId < 0) {
Matt Buckley124d0c672023-01-19 03:04:19 +0000130 return false;
131 }
Matt Buckleye9023cf2022-11-23 22:39:25 +0000132
133 // Assume that if we return before the end, it broke
134 mSessionValid = false;
135
136 ensureAPerformanceHintBindingInitialized();
137
138 APerformanceHintManager* manager = gAPH_getManagerFn();
139 if (!manager) return false;
140
141 std::vector<pid_t> tids = CommonPool::getThreadIds();
142 tids.push_back(mUiThreadId);
143 tids.push_back(mRenderThreadId);
144
145 // Use a placeholder target value to initialize,
146 // this will always be replaced elsewhere before it gets used
147 int64_t defaultTargetDurationNanos = 16666667;
Matt Buckley191f5cc2023-03-30 20:58:22 +0000148 mHintSessionFuture = CommonPool::async([=, tids = std::move(tids)] {
149 return gAPH_createSessionFn(manager, tids.data(), tids.size(), defaultTargetDurationNanos);
150 });
151 return false;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000152}
153
154void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000155 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000156 targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100;
157 if (targetWorkDurationNanos != mLastTargetWorkDuration &&
158 targetWorkDurationNanos > kSanityCheckLowerBound &&
159 targetWorkDurationNanos < kSanityCheckUpperBound) {
160 mLastTargetWorkDuration = targetWorkDurationNanos;
161 gAPH_updateTargetWorkDurationFn(mHintSession, targetWorkDurationNanos);
162 }
163 mLastFrameNotification = systemTime();
164}
165
166void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000167 if (!init()) return;
Matt Buckley814f9fc2023-05-03 14:32:11 +0000168 mResetsSinceLastReport = 0;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000169 if (actualDurationNanos > kSanityCheckLowerBound &&
170 actualDurationNanos < kSanityCheckUpperBound) {
171 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
172 }
173}
174
175void HintSessionWrapper::sendLoadResetHint() {
Matt Buckley814f9fc2023-05-03 14:32:11 +0000176 static constexpr int kMaxResetsSinceLastReport = 2;
Matt Buckley191f5cc2023-03-30 20:58:22 +0000177 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000178 nsecs_t now = systemTime();
Matt Buckley814f9fc2023-05-03 14:32:11 +0000179 if (now - mLastFrameNotification > kResetHintTimeout &&
180 mResetsSinceLastReport <= kMaxResetsSinceLastReport) {
181 ++mResetsSinceLastReport;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000182 gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_RESET));
183 }
184 mLastFrameNotification = now;
185}
186
Matt Buckleyac5f7552022-12-19 22:03:27 +0000187void HintSessionWrapper::sendLoadIncreaseHint() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000188 if (!init()) return;
Matt Buckleyac5f7552022-12-19 22:03:27 +0000189 gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_UP));
190}
191
Matt Buckleye9023cf2022-11-23 22:39:25 +0000192} /* namespace renderthread */
193} /* namespace uirenderer */
194} /* namespace android */