blob: 597cbf732d82278207240efd0989b0d57e3e5eed [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() {
96 if (mHintSession) {
97 gAPH_closeSessionFn(mHintSession);
98 }
99}
100
Matt Buckleye9023cf2022-11-23 22:39:25 +0000101bool HintSessionWrapper::init() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000102 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 Buckley124d0c672023-01-19 03:04:19 +0000118 // has bad argument values, don't even bother
Matt Buckley191f5cc2023-03-30 20:58:22 +0000119 if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() ||
120 mUiThreadId < 0 || mRenderThreadId < 0) {
Matt Buckley124d0c672023-01-19 03:04:19 +0000121 return false;
122 }
Matt Buckleye9023cf2022-11-23 22:39:25 +0000123
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 Buckley191f5cc2023-03-30 20:58:22 +0000139 mHintSessionFuture = CommonPool::async([=, tids = std::move(tids)] {
140 return gAPH_createSessionFn(manager, tids.data(), tids.size(), defaultTargetDurationNanos);
141 });
142 return false;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000143}
144
145void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000146 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000147 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
157void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000158 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000159 if (actualDurationNanos > kSanityCheckLowerBound &&
160 actualDurationNanos < kSanityCheckUpperBound) {
161 gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
162 }
163}
164
165void HintSessionWrapper::sendLoadResetHint() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000166 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000167 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 Buckleyac5f7552022-12-19 22:03:27 +0000174void HintSessionWrapper::sendLoadIncreaseHint() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000175 if (!init()) return;
Matt Buckleyac5f7552022-12-19 22:03:27 +0000176 gAPH_sendHintFn(mHintSession, static_cast<int>(SessionHint::CPU_LOAD_UP));
177}
178
Matt Buckleye9023cf2022-11-23 22:39:25 +0000179} /* namespace renderthread */
180} /* namespace uirenderer */
181} /* namespace android */