blob: 30e472a799268ae3d8f99ad1a23f4ad710bcfa3f [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 Buckley5fdfd602023-08-24 15:56:46 +000096 if (mHintSessionFuture.valid()) {
97 mHintSession = mHintSessionFuture.get();
98 }
Matt Buckleye9023cf2022-11-23 22:39:25 +000099 if (mHintSession) {
100 gAPH_closeSessionFn(mHintSession);
Matt Buckley5fdfd602023-08-24 15:56:46 +0000101 mSessionValid = true;
102 mHintSession = nullptr;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000103 }
104}
105
Matt Buckleye9023cf2022-11-23 22:39:25 +0000106bool HintSessionWrapper::init() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000107 if (mHintSession != nullptr) return true;
108
109 // If we're waiting for the session
110 if (mHintSessionFuture.valid()) {
111 // If the session is here
112 if (mHintSessionFuture.wait_for(0s) == std::future_status::ready) {
113 mHintSession = mHintSessionFuture.get();
114 if (mHintSession != nullptr) {
115 mSessionValid = true;
116 return true;
117 }
118 }
119 return false;
120 }
121
122 // If it broke last time we tried this, shouldn't be running, or
Matt Buckley124d0c672023-01-19 03:04:19 +0000123 // has bad argument values, don't even bother
Matt Buckley191f5cc2023-03-30 20:58:22 +0000124 if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() ||
125 mUiThreadId < 0 || mRenderThreadId < 0) {
Matt Buckley124d0c672023-01-19 03:04:19 +0000126 return false;
127 }
Matt Buckleye9023cf2022-11-23 22:39:25 +0000128
129 // Assume that if we return before the end, it broke
130 mSessionValid = false;
131
132 ensureAPerformanceHintBindingInitialized();
133
134 APerformanceHintManager* manager = gAPH_getManagerFn();
135 if (!manager) return false;
136
137 std::vector<pid_t> tids = CommonPool::getThreadIds();
138 tids.push_back(mUiThreadId);
139 tids.push_back(mRenderThreadId);
140
141 // Use a placeholder target value to initialize,
142 // this will always be replaced elsewhere before it gets used
143 int64_t defaultTargetDurationNanos = 16666667;
Matt Buckley191f5cc2023-03-30 20:58:22 +0000144 mHintSessionFuture = CommonPool::async([=, tids = std::move(tids)] {
145 return gAPH_createSessionFn(manager, tids.data(), tids.size(), defaultTargetDurationNanos);
146 });
147 return false;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000148}
149
150void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000151 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000152 targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100;
153 if (targetWorkDurationNanos != mLastTargetWorkDuration &&
154 targetWorkDurationNanos > kSanityCheckLowerBound &&
155 targetWorkDurationNanos < kSanityCheckUpperBound) {
156 mLastTargetWorkDuration = targetWorkDurationNanos;
157 gAPH_updateTargetWorkDurationFn(mHintSession, targetWorkDurationNanos);
158 }
159 mLastFrameNotification = systemTime();
160}
161
162void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000163 if (!init()) return;
Matt Buckley814f9fc2023-05-03 14:32:11 +0000164 mResetsSinceLastReport = 0;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000165 if (actualDurationNanos > kSanityCheckLowerBound &&
166 actualDurationNanos < kSanityCheckUpperBound) {
167 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
168 }
169}
170
171void HintSessionWrapper::sendLoadResetHint() {
Matt Buckley814f9fc2023-05-03 14:32:11 +0000172 static constexpr int kMaxResetsSinceLastReport = 2;
Matt Buckley191f5cc2023-03-30 20:58:22 +0000173 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000174 nsecs_t now = systemTime();
Matt Buckley814f9fc2023-05-03 14:32:11 +0000175 if (now - mLastFrameNotification > kResetHintTimeout &&
176 mResetsSinceLastReport <= kMaxResetsSinceLastReport) {
177 ++mResetsSinceLastReport;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000178 gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_RESET));
179 }
180 mLastFrameNotification = now;
181}
182
Matt Buckleyac5f7552022-12-19 22:03:27 +0000183void HintSessionWrapper::sendLoadIncreaseHint() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000184 if (!init()) return;
Matt Buckleyac5f7552022-12-19 22:03:27 +0000185 gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_UP));
186}
187
Matt Buckleye9023cf2022-11-23 22:39:25 +0000188} /* namespace renderthread */
189} /* namespace uirenderer */
190} /* namespace android */