blob: 859cc57dea9fd4605535bf2db711b87c70ba3ada [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>
Matt Buckley27da1342024-04-05 23:10:48 +000020#include <private/performance_hint_private.h>
Matt Buckleye9023cf2022-11-23 22:39:25 +000021
Matt Buckley191f5cc2023-03-30 20:58:22 +000022#include <future>
Matt Buckley0daae6a2023-09-14 22:56:50 +000023#include <optional>
Igor Kraskevich2cec1582024-03-13 11:23:40 +000024#include <vector>
Matt Buckley191f5cc2023-03-30 20:58:22 +000025
Matt Buckleye9023cf2022-11-23 22:39:25 +000026#include "utils/TimeUtils.h"
27
28namespace android {
29namespace uirenderer {
30
31namespace renderthread {
32
Matt Buckley0daae6a2023-09-14 22:56:50 +000033class RenderThread;
34
Matt Buckleye9023cf2022-11-23 22:39:25 +000035class HintSessionWrapper {
36public:
Matt Buckley0c668362023-09-07 05:52:07 +000037 friend class HintSessionWrapperTests;
38
Matt Buckleye9023cf2022-11-23 22:39:25 +000039 HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId);
40 ~HintSessionWrapper();
41
42 void updateTargetWorkDuration(long targetDurationNanos);
43 void reportActualWorkDuration(long actualDurationNanos);
44 void sendLoadResetHint();
Matt Buckleyac5f7552022-12-19 22:03:27 +000045 void sendLoadIncreaseHint();
Matt Buckley124d0c672023-01-19 03:04:19 +000046 bool init();
Matt Buckleyac620f62023-08-24 15:56:46 +000047 void destroy();
Matt Buckley0daae6a2023-09-14 22:56:50 +000048 bool alive();
49 nsecs_t getLastUpdate();
50 void delayedDestroy(renderthread::RenderThread& rt, nsecs_t delay,
51 std::shared_ptr<HintSessionWrapper> wrapperPtr);
Igor Kraskevich2cec1582024-03-13 11:23:40 +000052 // Must be called on Render thread. Otherwise can cause a race condition.
53 void setActiveFunctorThreads(std::vector<pid_t> threadIds);
Matt Buckleye9023cf2022-11-23 22:39:25 +000054
55private:
Matt Buckleye9023cf2022-11-23 22:39:25 +000056 APerformanceHintSession* mHintSession = nullptr;
Matt Buckley0daae6a2023-09-14 22:56:50 +000057 // This needs to work concurrently for testing
58 std::optional<std::shared_future<APerformanceHintSession*>> mHintSessionFuture;
Igor Kraskevich2cec1582024-03-13 11:23:40 +000059 // This needs to work concurrently for testing
60 std::optional<std::shared_future<int>> mSetThreadsFuture;
Matt Buckleye9023cf2022-11-23 22:39:25 +000061
Matt Buckley814f9fc2023-05-03 14:32:11 +000062 int mResetsSinceLastReport = 0;
Matt Buckleye9023cf2022-11-23 22:39:25 +000063 nsecs_t mLastFrameNotification = 0;
64 nsecs_t mLastTargetWorkDuration = 0;
65
66 pid_t mUiThreadId;
67 pid_t mRenderThreadId;
Igor Kraskevich2cec1582024-03-13 11:23:40 +000068 std::vector<pid_t> mPermanentSessionTids;
69 std::vector<pid_t> mActiveFunctorTids;
Matt Buckleye9023cf2022-11-23 22:39:25 +000070
71 bool mSessionValid = true;
72
73 static constexpr nsecs_t kResetHintTimeout = 100_ms;
74 static constexpr int64_t kSanityCheckLowerBound = 100_us;
75 static constexpr int64_t kSanityCheckUpperBound = 10_s;
Matt Buckley1b99d782023-09-26 19:30:25 +000076 static constexpr int64_t kDefaultTargetDuration = 16666667;
Matt Buckley0c668362023-09-07 05:52:07 +000077
78 // Allows easier stub when testing
79 class HintSessionBinding {
80 public:
81 virtual ~HintSessionBinding() = default;
82 virtual void init();
83 APerformanceHintManager* (*getManager)();
Matt Buckley27da1342024-04-05 23:10:48 +000084 APerformanceHintSession* (*createSessionInternal)(APerformanceHintManager* manager,
85 const int32_t* tids, size_t tidCount,
86 int64_t defaultTarget,
87 SessionTag tag) = nullptr;
Matt Buckley0c668362023-09-07 05:52:07 +000088 void (*closeSession)(APerformanceHintSession* session) = nullptr;
89 void (*updateTargetWorkDuration)(APerformanceHintSession* session,
90 int64_t targetDuration) = nullptr;
91 void (*reportActualWorkDuration)(APerformanceHintSession* session,
92 int64_t actualDuration) = nullptr;
93 void (*sendHint)(APerformanceHintSession* session, int32_t hintId) = nullptr;
Igor Kraskevich2cec1582024-03-13 11:23:40 +000094 int (*setThreads)(APerformanceHintSession* session, const pid_t* tids,
95 size_t size) = nullptr;
Matt Buckley0c668362023-09-07 05:52:07 +000096
97 private:
98 bool mInitialized = false;
99 };
100
101 std::shared_ptr<HintSessionBinding> mBinding;
Matt Buckleye9023cf2022-11-23 22:39:25 +0000102};
103
104} /* namespace renderthread */
105} /* namespace uirenderer */
106} /* namespace android */