blob: 36e91ea33c7518c6bf504b6d7f8a1f5a7063b12f [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 Buckley0c668362023-09-07 05:52:07 +000068
69 // Allows easier stub when testing
70 class HintSessionBinding {
71 public:
72 virtual ~HintSessionBinding() = default;
73 virtual void init();
74 APerformanceHintManager* (*getManager)();
75 APerformanceHintSession* (*createSession)(APerformanceHintManager* manager,
76 const int32_t* tids, size_t tidCount,
77 int64_t defaultTarget) = nullptr;
78 void (*closeSession)(APerformanceHintSession* session) = nullptr;
79 void (*updateTargetWorkDuration)(APerformanceHintSession* session,
80 int64_t targetDuration) = nullptr;
81 void (*reportActualWorkDuration)(APerformanceHintSession* session,
82 int64_t actualDuration) = nullptr;
83 void (*sendHint)(APerformanceHintSession* session, int32_t hintId) = nullptr;
84
85 private:
86 bool mInitialized = false;
87 };
88
89 std::shared_ptr<HintSessionBinding> mBinding;
Matt Buckleye9023cf2022-11-23 22:39:25 +000090};
91
92} /* namespace renderthread */
93} /* namespace uirenderer */
94} /* namespace android */