blob: d1ebe6d9f576012e40d759bdcea83dbf7c6676fd [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"
Matt Buckley0daae6a2023-09-14 22:56:50 +000027#include "RenderThread.h"
Matt Buckleye9023cf2022-11-23 22:39:25 +000028#include "thread/CommonPool.h"
29
Matt Buckley191f5cc2023-03-30 20:58:22 +000030using namespace std::chrono_literals;
31
Matt Buckleye9023cf2022-11-23 22:39:25 +000032namespace android {
33namespace uirenderer {
34namespace renderthread {
35
Matt Buckley0c668362023-09-07 05:52:07 +000036#define BIND_APH_METHOD(name) \
37 name = (decltype(name))dlsym(handle_, "APerformanceHint_" #name); \
38 LOG_ALWAYS_FATAL_IF(name == nullptr, "Failed to find required symbol APerformanceHint_" #name)
Matt Buckleye9023cf2022-11-23 22:39:25 +000039
Matt Buckley0c668362023-09-07 05:52:07 +000040void HintSessionWrapper::HintSessionBinding::init() {
41 if (mInitialized) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +000042
43 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
44 LOG_ALWAYS_FATAL_IF(handle_ == nullptr, "Failed to dlopen libandroid.so!");
45
Matt Buckley0c668362023-09-07 05:52:07 +000046 BIND_APH_METHOD(getManager);
47 BIND_APH_METHOD(createSession);
48 BIND_APH_METHOD(closeSession);
49 BIND_APH_METHOD(updateTargetWorkDuration);
50 BIND_APH_METHOD(reportActualWorkDuration);
51 BIND_APH_METHOD(sendHint);
Matt Buckleye9023cf2022-11-23 22:39:25 +000052
Matt Buckley0c668362023-09-07 05:52:07 +000053 mInitialized = true;
Matt Buckleye9023cf2022-11-23 22:39:25 +000054}
55
Matt Buckleye9023cf2022-11-23 22:39:25 +000056HintSessionWrapper::HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId)
Matt Buckley0c668362023-09-07 05:52:07 +000057 : mUiThreadId(uiThreadId)
58 , mRenderThreadId(renderThreadId)
59 , mBinding(std::make_shared<HintSessionBinding>()) {}
Matt Buckleye9023cf2022-11-23 22:39:25 +000060
61HintSessionWrapper::~HintSessionWrapper() {
Matt Buckleyac620f62023-08-24 15:56:46 +000062 destroy();
63}
64
65void HintSessionWrapper::destroy() {
Matt Buckley0daae6a2023-09-14 22:56:50 +000066 if (mHintSessionFuture.has_value()) {
67 mHintSession = mHintSessionFuture->get();
68 mHintSessionFuture = std::nullopt;
Matt Buckleyac620f62023-08-24 15:56:46 +000069 }
Matt Buckleye9023cf2022-11-23 22:39:25 +000070 if (mHintSession) {
Matt Buckley0c668362023-09-07 05:52:07 +000071 mBinding->closeSession(mHintSession);
Matt Buckleyac620f62023-08-24 15:56:46 +000072 mSessionValid = true;
73 mHintSession = nullptr;
Matt Buckleye9023cf2022-11-23 22:39:25 +000074 }
75}
76
Matt Buckleye9023cf2022-11-23 22:39:25 +000077bool HintSessionWrapper::init() {
Matt Buckley191f5cc2023-03-30 20:58:22 +000078 if (mHintSession != nullptr) return true;
Matt Buckley191f5cc2023-03-30 20:58:22 +000079 // If we're waiting for the session
Matt Buckley0daae6a2023-09-14 22:56:50 +000080 if (mHintSessionFuture.has_value()) {
Matt Buckley191f5cc2023-03-30 20:58:22 +000081 // If the session is here
Matt Buckley0daae6a2023-09-14 22:56:50 +000082 if (mHintSessionFuture->wait_for(0s) == std::future_status::ready) {
83 mHintSession = mHintSessionFuture->get();
84 mHintSessionFuture = std::nullopt;
Matt Buckley191f5cc2023-03-30 20:58:22 +000085 if (mHintSession != nullptr) {
86 mSessionValid = true;
87 return true;
88 }
89 }
90 return false;
91 }
92
93 // If it broke last time we tried this, shouldn't be running, or
Matt Buckley124d0c672023-01-19 03:04:19 +000094 // has bad argument values, don't even bother
Matt Buckley191f5cc2023-03-30 20:58:22 +000095 if (!mSessionValid || !Properties::useHintManager || !Properties::isDrawingEnabled() ||
96 mUiThreadId < 0 || mRenderThreadId < 0) {
Matt Buckley124d0c672023-01-19 03:04:19 +000097 return false;
98 }
Matt Buckleye9023cf2022-11-23 22:39:25 +000099
100 // Assume that if we return before the end, it broke
101 mSessionValid = false;
102
Matt Buckley0c668362023-09-07 05:52:07 +0000103 mBinding->init();
Matt Buckleye9023cf2022-11-23 22:39:25 +0000104
Matt Buckley0c668362023-09-07 05:52:07 +0000105 APerformanceHintManager* manager = mBinding->getManager();
Matt Buckleye9023cf2022-11-23 22:39:25 +0000106 if (!manager) return false;
107
108 std::vector<pid_t> tids = CommonPool::getThreadIds();
109 tids.push_back(mUiThreadId);
110 tids.push_back(mRenderThreadId);
111
112 // Use a placeholder target value to initialize,
113 // this will always be replaced elsewhere before it gets used
114 int64_t defaultTargetDurationNanos = 16666667;
Matt Buckley0c668362023-09-07 05:52:07 +0000115 mHintSessionFuture = CommonPool::async([=, this, tids = std::move(tids)] {
116 return mBinding->createSession(manager, tids.data(), tids.size(),
117 defaultTargetDurationNanos);
Matt Buckley191f5cc2023-03-30 20:58:22 +0000118 });
119 return false;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000120}
121
122void HintSessionWrapper::updateTargetWorkDuration(long targetWorkDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000123 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000124 targetWorkDurationNanos = targetWorkDurationNanos * Properties::targetCpuTimePercentage / 100;
125 if (targetWorkDurationNanos != mLastTargetWorkDuration &&
126 targetWorkDurationNanos > kSanityCheckLowerBound &&
127 targetWorkDurationNanos < kSanityCheckUpperBound) {
128 mLastTargetWorkDuration = targetWorkDurationNanos;
Matt Buckley0c668362023-09-07 05:52:07 +0000129 mBinding->updateTargetWorkDuration(mHintSession, targetWorkDurationNanos);
Matt Buckleye9023cf2022-11-23 22:39:25 +0000130 }
131 mLastFrameNotification = systemTime();
132}
133
134void HintSessionWrapper::reportActualWorkDuration(long actualDurationNanos) {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000135 if (!init()) return;
Matt Buckley814f9fc2023-05-03 14:32:11 +0000136 mResetsSinceLastReport = 0;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000137 if (actualDurationNanos > kSanityCheckLowerBound &&
138 actualDurationNanos < kSanityCheckUpperBound) {
Matt Buckley0c668362023-09-07 05:52:07 +0000139 mBinding->reportActualWorkDuration(mHintSession, actualDurationNanos);
Matt Buckleye9023cf2022-11-23 22:39:25 +0000140 }
Matt Buckley0daae6a2023-09-14 22:56:50 +0000141 mLastFrameNotification = systemTime();
Matt Buckleye9023cf2022-11-23 22:39:25 +0000142}
143
144void HintSessionWrapper::sendLoadResetHint() {
Matt Buckley814f9fc2023-05-03 14:32:11 +0000145 static constexpr int kMaxResetsSinceLastReport = 2;
Matt Buckley191f5cc2023-03-30 20:58:22 +0000146 if (!init()) return;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000147 nsecs_t now = systemTime();
Matt Buckley814f9fc2023-05-03 14:32:11 +0000148 if (now - mLastFrameNotification > kResetHintTimeout &&
149 mResetsSinceLastReport <= kMaxResetsSinceLastReport) {
150 ++mResetsSinceLastReport;
Matt Buckley0c668362023-09-07 05:52:07 +0000151 mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_RESET));
Matt Buckleye9023cf2022-11-23 22:39:25 +0000152 }
153 mLastFrameNotification = now;
154}
155
Matt Buckleyac5f7552022-12-19 22:03:27 +0000156void HintSessionWrapper::sendLoadIncreaseHint() {
Matt Buckley191f5cc2023-03-30 20:58:22 +0000157 if (!init()) return;
Matt Buckley0c668362023-09-07 05:52:07 +0000158 mBinding->sendHint(mHintSession, static_cast<int32_t>(SessionHint::CPU_LOAD_UP));
Matt Buckley0daae6a2023-09-14 22:56:50 +0000159 mLastFrameNotification = systemTime();
160}
161
162bool HintSessionWrapper::alive() {
163 return mHintSession != nullptr;
164}
165
166nsecs_t HintSessionWrapper::getLastUpdate() {
167 return mLastFrameNotification;
168}
169
170// Requires passing in its shared_ptr since it shouldn't own a shared_ptr to itself
171void HintSessionWrapper::delayedDestroy(RenderThread& rt, nsecs_t delay,
172 std::shared_ptr<HintSessionWrapper> wrapperPtr) {
173 nsecs_t lastUpdate = wrapperPtr->getLastUpdate();
174 rt.queue().postDelayed(delay, [lastUpdate = lastUpdate, wrapper = wrapperPtr]() mutable {
175 if (wrapper->getLastUpdate() == lastUpdate) {
176 wrapper->destroy();
177 }
178 // Ensure the shared_ptr is killed at the end of the method
179 wrapper = nullptr;
180 });
Matt Buckleyac5f7552022-12-19 22:03:27 +0000181}
182
Matt Buckleye9023cf2022-11-23 22:39:25 +0000183} /* namespace renderthread */
184} /* namespace uirenderer */
185} /* namespace android */