| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 1 | /* | 
|  | 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 Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 21 | #include <future> | 
| Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 22 | #include <optional> | 
| Matt Buckley | 191f5cc | 2023-03-30 20:58:22 +0000 | [diff] [blame] | 23 |  | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 24 | #include "utils/TimeUtils.h" | 
|  | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 | namespace uirenderer { | 
|  | 28 |  | 
|  | 29 | namespace renderthread { | 
|  | 30 |  | 
| Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 31 | class RenderThread; | 
|  | 32 |  | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 33 | class HintSessionWrapper { | 
|  | 34 | public: | 
| Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 35 | friend class HintSessionWrapperTests; | 
|  | 36 |  | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 37 | HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId); | 
|  | 38 | ~HintSessionWrapper(); | 
|  | 39 |  | 
|  | 40 | void updateTargetWorkDuration(long targetDurationNanos); | 
|  | 41 | void reportActualWorkDuration(long actualDurationNanos); | 
|  | 42 | void sendLoadResetHint(); | 
| Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 43 | void sendLoadIncreaseHint(); | 
| Matt Buckley | 124d0c67 | 2023-01-19 03:04:19 +0000 | [diff] [blame] | 44 | bool init(); | 
| Matt Buckley | ac620f6 | 2023-08-24 15:56:46 +0000 | [diff] [blame] | 45 | void destroy(); | 
| Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 46 | bool alive(); | 
|  | 47 | nsecs_t getLastUpdate(); | 
|  | 48 | void delayedDestroy(renderthread::RenderThread& rt, nsecs_t delay, | 
|  | 49 | std::shared_ptr<HintSessionWrapper> wrapperPtr); | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 50 |  | 
|  | 51 | private: | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 52 | APerformanceHintSession* mHintSession = nullptr; | 
| Matt Buckley | 0daae6a | 2023-09-14 22:56:50 +0000 | [diff] [blame] | 53 | // This needs to work concurrently for testing | 
|  | 54 | std::optional<std::shared_future<APerformanceHintSession*>> mHintSessionFuture; | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 55 |  | 
| Matt Buckley | 814f9fc | 2023-05-03 14:32:11 +0000 | [diff] [blame] | 56 | int mResetsSinceLastReport = 0; | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 57 | 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 Buckley | 1b99d78 | 2023-09-26 19:30:25 +0000 | [diff] [blame] | 68 | static constexpr int64_t kDefaultTargetDuration = 16666667; | 
| Matt Buckley | 0c66836 | 2023-09-07 05:52:07 +0000 | [diff] [blame] | 69 |  | 
|  | 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 Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 91 | }; | 
|  | 92 |  | 
|  | 93 | } /* namespace renderthread */ | 
|  | 94 | } /* namespace uirenderer */ | 
|  | 95 | } /* namespace android */ |