1 /*
2 * Copyright 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 "DisplayHardware/DisplayMode.h"
20
21 namespace android::mock {
22
23 inline DisplayMode::Builder createDisplayModeBuilder(
24 DisplayModeId modeId, Fps displayRefreshRate, int32_t group = 0,
25 ui::Size resolution = ui::Size(1920, 1080),
26 PhysicalDisplayId displayId = PhysicalDisplayId::fromPort(0)) {
27 return DisplayMode::Builder(hal::HWConfigId(ftl::to_underlying(modeId)))
28 .setId(modeId)
29 .setPhysicalDisplayId(displayId)
30 .setVsyncPeriod(displayRefreshRate.getPeriodNsecs())
31 .setGroup(group)
32 .setResolution(resolution);
33 }
34
35 inline DisplayModePtr createDisplayMode(
36 DisplayModeId modeId, Fps refreshRate, int32_t group = 0,
37 ui::Size resolution = ui::Size(1920, 1080),
38 PhysicalDisplayId displayId = PhysicalDisplayId::fromPort(0)) {
39 return createDisplayModeBuilder(modeId, refreshRate, group, resolution, displayId).build();
40 }
41
createDisplayMode(PhysicalDisplayId displayId,DisplayModeId modeId,Fps refreshRate)42 inline DisplayModePtr createDisplayMode(PhysicalDisplayId displayId, DisplayModeId modeId,
43 Fps refreshRate) {
44 return createDisplayMode(modeId, refreshRate, {}, {}, displayId);
45 }
46
47 inline DisplayModePtr createVrrDisplayMode(
48 DisplayModeId modeId, Fps displayRefreshRate, std::optional<hal::VrrConfig> vrrConfig,
49 int32_t group = 0, ui::Size resolution = ui::Size(1920, 1080),
50 PhysicalDisplayId displayId = PhysicalDisplayId::fromPort(0)) {
51 return createDisplayModeBuilder(modeId, displayRefreshRate, group, resolution, displayId)
52 .setVrrConfig(std::move(vrrConfig))
53 .build();
54 }
55
cloneForDisplay(PhysicalDisplayId displayId,const DisplayModePtr & modePtr)56 inline DisplayModePtr cloneForDisplay(PhysicalDisplayId displayId, const DisplayModePtr& modePtr) {
57 return DisplayMode::Builder(modePtr->getHwcId())
58 .setId(modePtr->getId())
59 .setPhysicalDisplayId(displayId)
60 .setVsyncPeriod(modePtr->getVsyncRate().getPeriodNsecs())
61 .setGroup(modePtr->getGroup())
62 .setResolution(modePtr->getResolution())
63 .build();
64 }
65
cloneForDisplay(PhysicalDisplayId displayId,const DisplayModes & modes)66 inline DisplayModes cloneForDisplay(PhysicalDisplayId displayId, const DisplayModes& modes) {
67 DisplayModes clones;
68
69 for (const auto& [id, modePtr] : modes) {
70 clones.try_emplace(id, cloneForDisplay(displayId, modePtr));
71 }
72
73 return clones;
74 }
75
76 } // namespace android::mock
77