blob: 41891cd80a42bcbd8f0a27a67c6e85bc56166645 [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>
Matt Buckley191f5cc2023-03-30 20:58:22 +000023
Matt Buckleye9023cf2022-11-23 22:39:25 +000024#include "utils/TimeUtils.h"
25
26namespace android {
27namespace uirenderer {
28
29namespace renderthread {
30
Matt Buckley0daae6a2023-09-14 22:56:50 +000031class RenderThread;
32
Matt Buckleye9023cf2022-11-23 22:39:25 +000033class HintSessionWrapper {
34public:
Matt Buckley0c668362023-09-07 05:52:07 +000035 friend class HintSessionWrapperTests;
36
Matt Buckleye9023cf2022-11-23 22:39:25 +000037 HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId);
38 ~HintSessionWrapper();
39
40 void updateTargetWorkDuration(long targetDurationNanos);
41 void reportActualWorkDuration(long actualDurationNanos);
42 void sendLoadResetHint();
Matt Buckleyac5f7552022-12-19 22:03:27 +000043 void sendLoadIncreaseHint();
Matt Buckley124d0c672023-01-19 03:04:19 +000044 bool init();
Matt Buckleyac620f62023-08-24 15:56:46 +000045 void destroy();
Matt Buckley0daae6a2023-09-14 22:56:50 +000046 bool alive();
47 nsecs_t getLastUpdate();
48 void delayedDestroy(renderthread::RenderThread& rt, nsecs_t delay,
49 std::shared_ptr<HintSessionWrapper> wrapperPtr);
Matt Buckleye9023cf2022-11-23 22:39:25 +000050
51private:
Matt Buckleye9023cf2022-11-23 22:39:25 +000052 APerformanceHintSession* mHintSession = nullptr;
Matt Buckley0daae6a2023-09-14 22:56:50 +000053 // This needs to work concurrently for testing
54 std::optional<std::shared_future<APerformanceHintSession*>> mHintSessionFuture;
Matt Buckleye9023cf2022-11-23 22:39:25 +000055
Matt Buckley814f9fc2023-05-03 14:32:11 +000056 int mResetsSinceLastReport = 0;
Matt Buckleye9023cf2022-11-23 22:39:25 +000057 nsecs_t mLastFrameNotification = 0;
58 nsecs_t mLastTargetWorkDuration = 0;
59
60 pid_t mUiThreadId;
61 pid_t mRenderThreadId;
62
63 bool mSessionValid = true;
64
65 static constexpr nsecs_t kResetHintTimeout = 100_ms;
66 static constexpr int64_t kSanityCheckLowerBound = 100_us;
67 static constexpr int64_t kSanityCheckUpperBound = 10_s;
Matt Buckley1b99d782023-09-26 19:30:25 +000068 static constexpr int64_t kDefaultTargetDuration = 16666667;
Matt Buckley0c668362023-09-07 05:52:07 +000069
70 // Allows easier stub when testing
71 class HintSessionBinding {
72 public:
73 virtual ~HintSessionBinding() = default;
74 virtual void init();
75 APerformanceHintManager* (*getManager)();
76 APerformanceHintSession* (*createSession)(APerformanceHintManager* manager,
77 const int32_t* tids, size_t tidCount,
78 int64_t defaultTarget) = nullptr;
79 void (*closeSession)(APerformanceHintSession* session) = nullptr;
80 void (*updateTargetWorkDuration)(APerformanceHintSession* session,
81 int64_t targetDuration) = nullptr;
82 void (*reportActualWorkDuration)(APerformanceHintSession* session,
83 int64_t actualDuration) = nullptr;
84 void (*sendHint)(APerformanceHintSession* session, int32_t hintId) = nullptr;
85
86 private:
87 bool mInitialized = false;
88 };
89
90 std::shared_ptr<HintSessionBinding> mBinding;
Matt Buckleye9023cf2022-11-23 22:39:25 +000091};
92
93} /* namespace renderthread */
94} /* namespace uirenderer */
95} /* namespace android */