blob: b6a6bee714f5f8e7e68f65e5411da2722d3f34f8 [file] [log] [blame]
Dominik Laskowski6e465152022-09-28 11:00:25 -04001/*
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>
20#include <utility>
21
22#include <android-base/thread_annotations.h>
23#include <ui/DisplayId.h>
24#include <ui/DisplayMap.h>
25
26#include "Display/DisplaySnapshotRef.h"
27#include "DisplayHardware/DisplayMode.h"
28#include "Scheduler/RefreshRateSelector.h"
29#include "ThreadContext.h"
30
31namespace android::display {
32
33// Selects the DisplayMode of each physical display, in accordance with DisplayManager policy and
34// certain heuristic signals.
35class DisplayModeController {
36public:
37 // The referenced DisplaySnapshot must outlive the registration.
38 void registerDisplay(DisplaySnapshotRef, DisplayModeId, scheduler::RefreshRateSelector::Config)
39 REQUIRES(kMainThreadContext);
40 void unregisterDisplay(PhysicalDisplayId) REQUIRES(kMainThreadContext);
41
42 // TODO(b/241285876): Remove once ownership is no longer shared with DisplayDevice.
43 using RefreshRateSelectorPtr = std::shared_ptr<scheduler::RefreshRateSelector>;
44
45 // Returns `nullptr` if the display is no longer registered (or never was).
46 RefreshRateSelectorPtr selectorPtrFor(PhysicalDisplayId) REQUIRES(kMainThreadContext);
47
48 // Used by tests to inject an existing RefreshRateSelector.
49 // TODO(b/241285876): Remove this.
50 void registerDisplay(PhysicalDisplayId displayId, DisplaySnapshotRef snapshotRef,
51 RefreshRateSelectorPtr selectorPtr) {
52 mDisplays.emplace_or_replace(displayId, snapshotRef, selectorPtr);
53 }
54
55private:
56 struct Display {
57 Display(DisplaySnapshotRef snapshot, RefreshRateSelectorPtr selectorPtr)
58 : snapshot(snapshot), selectorPtr(std::move(selectorPtr)) {}
59
60 Display(DisplaySnapshotRef snapshot, DisplayModes modes, DisplayModeId activeModeId,
61 scheduler::RefreshRateSelector::Config config)
62 : Display(snapshot,
63 std::make_shared<scheduler::RefreshRateSelector>(std::move(modes),
64 activeModeId, config)) {}
65
66 const DisplaySnapshotRef snapshot;
67 const RefreshRateSelectorPtr selectorPtr;
68 };
69
70 ui::PhysicalDisplayMap<PhysicalDisplayId, Display> mDisplays;
71};
72
73} // namespace android::display