blob: 14e7a53fd94fc79e730a452d4e0cc71ac42a9b6d [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#pragma once
18
19#include <android/performance_hint.h>
20
Matt Buckley191f5cc2023-03-30 20:58:22 +000021#include <future>
Matt Buckley0daae6a2023-09-14 22:56:50 +000022#include <optional>
Igor Kraskevich2cec1582024-03-13 11:23:40 +000023#include <vector>
Matt Buckley191f5cc2023-03-30 20:58:22 +000024
Matt Buckleye9023cf2022-11-23 22:39:25 +000025#include "utils/TimeUtils.h"
26
27namespace android {
28namespace uirenderer {
29
30namespace renderthread {
31
Matt Buckley0daae6a2023-09-14 22:56:50 +000032class RenderThread;
33
Matt Buckleye9023cf2022-11-23 22:39:25 +000034class HintSessionWrapper {
35public:
Matt Buckley0c668362023-09-07 05:52:07 +000036 friend class HintSessionWrapperTests;
37
Matt Buckleye9023cf2022-11-23 22:39:25 +000038 HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId);
39 ~HintSessionWrapper();
40
41 void updateTargetWorkDuration(long targetDurationNanos);
42 void reportActualWorkDuration(long actualDurationNanos);
43 void sendLoadResetHint();
Matt Buckleyac5f7552022-12-19 22:03:27 +000044 void sendLoadIncreaseHint();
Matt Buckley124d0c672023-01-19 03:04:19 +000045 bool init();
Matt Buckleyac620f62023-08-24 15:56:46 +000046 void destroy();
Matt Buckley0daae6a2023-09-14 22:56:50 +000047 bool alive();
48 nsecs_t getLastUpdate();
49 void delayedDestroy(renderthread::RenderThread& rt, nsecs_t delay,
50 std::shared_ptr<HintSessionWrapper> wrapperPtr);
Igor Kraskevich2cec1582024-03-13 11:23:40 +000051 // Must be called on Render thread. Otherwise can cause a race condition.
52 void setActiveFunctorThreads(std::vector<pid_t> threadIds);
Matt Buckleye9023cf2022-11-23 22:39:25 +000053
54private:
Matt Buckleye9023cf2022-11-23 22:39:25 +000055 APerformanceHintSession* mHintSession = nullptr;
Matt Buckley0daae6a2023-09-14 22:56:50 +000056 // This needs to work concurrently for testing
57 std::optional<std::shared_future<APerformanceHintSession*>> mHintSessionFuture;
Igor Kraskevich2cec1582024-03-13 11:23:40 +000058 // This needs to work concurrently for testing
59 std::optional<std::shared_future<int>> mSetThreadsFuture;
Matt Buckleye9023cf2022-11-23 22:39:25 +000060
Matt Buckley814f9fc2023-05-03 14:32:11 +000061 int mResetsSinceLastReport = 0;
Matt Buckleye9023cf2022-11-23 22:39:25 +000062 nsecs_t mLastFrameNotification = 0;
63 nsecs_t mLastTargetWorkDuration = 0;
64
65 pid_t mUiThreadId;
66 pid_t mRenderThreadId;
Igor Kraskevich2cec1582024-03-13 11:23:40 +000067 std::vector<pid_t> mPermanentSessionTids;
68 std::vector<pid_t> mActiveFunctorTids;
Matt Buckleye9023cf2022-11-23 22:39:25 +000069
70 bool mSessionValid = true;
71
72 static constexpr nsecs_t kResetHintTimeout = 100_ms;
73 static constexpr int64_t kSanityCheckLowerBound = 100_us;
74 static constexpr int64_t kSanityCheckUpperBound = 10_s;
Matt Buckley1b99d782023-09-26 19:30:25 +000075 static constexpr int64_t kDefaultTargetDuration = 16666667;
Matt Buckley0c668362023-09-07 05:52:07 +000076
77 // Allows easier stub when testing
78 class HintSessionBinding {
79 public:
80 virtual ~HintSessionBinding() = default;
81 virtual void init();
82 APerformanceHintManager* (*getManager)();
83 APerformanceHintSession* (*createSession)(APerformanceHintManager* manager,
84 const int32_t* tids, size_t tidCount,
85 int64_t defaultTarget) = nullptr;
86 void (*closeSession)(APerformanceHintSession* session) = nullptr;
87 void (*updateTargetWorkDuration)(APerformanceHintSession* session,
88 int64_t targetDuration) = nullptr;
89 void (*reportActualWorkDuration)(APerformanceHintSession* session,
90 int64_t actualDuration) = nullptr;
91 void (*sendHint)(APerformanceHintSession* session, int32_t hintId) = nullptr;
Igor Kraskevich2cec1582024-03-13 11:23:40 +000092 int (*setThreads)(APerformanceHintSession* session, const pid_t* tids,
93 size_t size) = nullptr;
Matt Buckley0c668362023-09-07 05:52:07 +000094
95 private:
96 bool mInitialized = false;
97 };
98
99 std::shared_ptr<HintSessionBinding> mBinding;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000100};
101
102} /* namespace renderthread */
103} /* namespace uirenderer */
104} /* namespace android */