Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 <memory> |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 20 | #include <mutex> |
| 21 | #include <string> |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
| 24 | #include <android-base/thread_annotations.h> |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 25 | #include <ftl/function.h> |
| 26 | #include <ftl/optional.h> |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 27 | #include <ui/DisplayId.h> |
| 28 | #include <ui/DisplayMap.h> |
| 29 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 30 | #include "Display/DisplayModeRequest.h" |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 31 | #include "Display/DisplaySnapshotRef.h" |
| 32 | #include "DisplayHardware/DisplayMode.h" |
| 33 | #include "Scheduler/RefreshRateSelector.h" |
| 34 | #include "ThreadContext.h" |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 35 | #include "TracedOrdinal.h" |
| 36 | |
| 37 | namespace android { |
| 38 | class HWComposer; |
| 39 | } // namespace android |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 40 | |
| 41 | namespace android::display { |
| 42 | |
| 43 | // Selects the DisplayMode of each physical display, in accordance with DisplayManager policy and |
| 44 | // certain heuristic signals. |
| 45 | class DisplayModeController { |
| 46 | public: |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 47 | using ActiveModeListener = ftl::Function<void(PhysicalDisplayId, Fps vsyncRate, Fps renderFps)>; |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 48 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 49 | DisplayModeController() = default; |
| 50 | |
| 51 | void setHwComposer(HWComposer* composerPtr) { mComposerPtr = composerPtr; } |
| 52 | void setActiveModeListener(const ActiveModeListener& listener) { |
| 53 | mActiveModeListener = listener; |
| 54 | } |
| 55 | |
| 56 | // TODO: b/241285876 - Remove once ownership is no longer shared with DisplayDevice. |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 57 | using RefreshRateSelectorPtr = std::shared_ptr<scheduler::RefreshRateSelector>; |
| 58 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 59 | // Used by tests to inject an existing RefreshRateSelector. |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 60 | // TODO: b/241285876 - Remove this. |
| 61 | void registerDisplay(PhysicalDisplayId, DisplaySnapshotRef, RefreshRateSelectorPtr) |
| 62 | EXCLUDES(mDisplayLock); |
| 63 | |
| 64 | // The referenced DisplaySnapshot must outlive the registration. |
| 65 | void registerDisplay(DisplaySnapshotRef, DisplayModeId, scheduler::RefreshRateSelector::Config) |
| 66 | REQUIRES(kMainThreadContext) EXCLUDES(mDisplayLock); |
| 67 | void unregisterDisplay(PhysicalDisplayId) REQUIRES(kMainThreadContext) EXCLUDES(mDisplayLock); |
| 68 | |
| 69 | // Returns `nullptr` if the display is no longer registered (or never was). |
| 70 | RefreshRateSelectorPtr selectorPtrFor(PhysicalDisplayId) const EXCLUDES(mDisplayLock); |
| 71 | |
| 72 | enum class DesiredModeAction { None, InitiateDisplayModeSwitch, InitiateRenderRateSwitch }; |
Manasi Navare | fc2a4a7 | 2024-11-12 23:59:21 +0000 | [diff] [blame] | 73 | enum class ModeChangeResult { Changed, Rejected, Aborted }; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 74 | |
| 75 | DesiredModeAction setDesiredMode(PhysicalDisplayId, DisplayModeRequest&&) |
| 76 | EXCLUDES(mDisplayLock); |
| 77 | |
| 78 | using DisplayModeRequestOpt = ftl::Optional<DisplayModeRequest>; |
| 79 | |
| 80 | DisplayModeRequestOpt getDesiredMode(PhysicalDisplayId) const EXCLUDES(mDisplayLock); |
| 81 | void clearDesiredMode(PhysicalDisplayId) EXCLUDES(mDisplayLock); |
| 82 | |
| 83 | DisplayModeRequestOpt getPendingMode(PhysicalDisplayId) const REQUIRES(kMainThreadContext) |
| 84 | EXCLUDES(mDisplayLock); |
| 85 | bool isModeSetPending(PhysicalDisplayId) const REQUIRES(kMainThreadContext) |
| 86 | EXCLUDES(mDisplayLock); |
| 87 | |
| 88 | scheduler::FrameRateMode getActiveMode(PhysicalDisplayId) const EXCLUDES(mDisplayLock); |
| 89 | |
Manasi Navare | fc2a4a7 | 2024-11-12 23:59:21 +0000 | [diff] [blame] | 90 | ModeChangeResult initiateModeChange(PhysicalDisplayId, DisplayModeRequest&&, |
| 91 | const hal::VsyncPeriodChangeConstraints&, |
| 92 | hal::VsyncPeriodChangeTimeline& outTimeline) |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 93 | REQUIRES(kMainThreadContext) EXCLUDES(mDisplayLock); |
| 94 | |
| 95 | void finalizeModeChange(PhysicalDisplayId, DisplayModeId, Fps vsyncRate, Fps renderFps) |
| 96 | REQUIRES(kMainThreadContext) EXCLUDES(mDisplayLock); |
| 97 | |
| 98 | void setActiveMode(PhysicalDisplayId, DisplayModeId, Fps vsyncRate, Fps renderFps) |
| 99 | EXCLUDES(mDisplayLock); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 100 | |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 101 | void updateKernelIdleTimer(PhysicalDisplayId) REQUIRES(kMainThreadContext) |
| 102 | EXCLUDES(mDisplayLock); |
| 103 | |
| 104 | struct KernelIdleTimerState { |
| 105 | std::optional<DisplayModeId> desiredModeIdOpt = std::nullopt; |
| 106 | bool isEnabled = false; |
| 107 | }; |
| 108 | |
| 109 | KernelIdleTimerState getKernelIdleTimerState(PhysicalDisplayId) const |
| 110 | REQUIRES(kMainThreadContext) EXCLUDES(mDisplayLock); |
| 111 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 112 | private: |
| 113 | struct Display { |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 114 | template <size_t N> |
| 115 | std::string concatId(const char (&)[N]) const; |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 116 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 117 | Display(DisplaySnapshotRef, RefreshRateSelectorPtr); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 118 | Display(DisplaySnapshotRef snapshot, DisplayModes modes, DisplayModeId activeModeId, |
| 119 | scheduler::RefreshRateSelector::Config config) |
| 120 | : Display(snapshot, |
| 121 | std::make_shared<scheduler::RefreshRateSelector>(std::move(modes), |
| 122 | activeModeId, config)) {} |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 123 | const DisplaySnapshotRef snapshot; |
| 124 | const RefreshRateSelectorPtr selectorPtr; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 125 | |
| 126 | const std::string pendingModeFpsTrace; |
| 127 | const std::string activeModeFpsTrace; |
| 128 | const std::string renderRateFpsTrace; |
| 129 | |
| 130 | std::mutex desiredModeLock; |
| 131 | DisplayModeRequestOpt desiredModeOpt GUARDED_BY(desiredModeLock); |
| 132 | TracedOrdinal<bool> hasDesiredModeTrace GUARDED_BY(desiredModeLock); |
| 133 | |
| 134 | DisplayModeRequestOpt pendingModeOpt GUARDED_BY(kMainThreadContext); |
| 135 | bool isModeSetPending GUARDED_BY(kMainThreadContext) = false; |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 136 | |
| 137 | bool isKernelIdleTimerEnabled GUARDED_BY(kMainThreadContext) = false; |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 138 | }; |
| 139 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 140 | using DisplayPtr = std::unique_ptr<Display>; |
| 141 | |
| 142 | void setActiveModeLocked(PhysicalDisplayId, DisplayModeId, Fps vsyncRate, Fps renderFps) |
| 143 | REQUIRES(mDisplayLock); |
| 144 | |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 145 | using KernelIdleTimerController = scheduler::RefreshRateSelector::KernelIdleTimerController; |
| 146 | void updateKernelIdleTimer(PhysicalDisplayId, std::chrono::milliseconds timeout, |
| 147 | KernelIdleTimerController) REQUIRES(mDisplayLock); |
| 148 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 149 | // Set once when initializing the DisplayModeController, which the HWComposer must outlive. |
| 150 | HWComposer* mComposerPtr = nullptr; |
| 151 | |
| 152 | ActiveModeListener mActiveModeListener; |
| 153 | |
| 154 | mutable std::mutex mDisplayLock; |
| 155 | ui::PhysicalDisplayMap<PhysicalDisplayId, DisplayPtr> mDisplays GUARDED_BY(mDisplayLock); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | } // namespace android::display |