blob: 0ab9605fca4516f4935c618c71c9b4377bd13a33 [file] [log] [blame]
Marin Shalamanov3ea1d602020-12-16 19:59:39 +01001/*
2 * Copyright 2020 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
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080019#include <cstddef>
20#include <memory>
21#include <vector>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010022
Marin Shalamanov5801c942020-12-17 17:00:13 +010023#include <android-base/stringprintf.h>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010024#include <android/configuration.h>
Ady Abraham5e7ee862021-06-23 17:43:41 -070025#include <ui/DisplayId.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010026#include <ui/DisplayMode.h>
Marin Shalamanov045b7002021-01-07 16:56:24 +010027#include <ui/Size.h>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010028#include <utils/Timers.h>
29
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080030#include <scheduler/Fps.h>
31
32#include "DisplayHardware/Hal.h"
33#include "Scheduler/StrongTyping.h"
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010034
35namespace android {
36
37namespace hal = android::hardware::graphics::composer::hal;
38
39class DisplayMode;
40using DisplayModePtr = std::shared_ptr<const DisplayMode>;
41using DisplayModes = std::vector<DisplayModePtr>;
Marin Shalamanov228f46b2021-01-28 21:11:45 +010042using DisplayModeId = StrongTyping<ui::DisplayModeId, struct DisplayModeIdTag, Compare, Hash>;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010043
44class DisplayMode {
45public:
46 class Builder {
47 public:
48 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
49
50 DisplayModePtr build() {
51 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
52 }
53
Marin Shalamanov23c44202020-12-22 19:09:20 +010054 Builder& setId(DisplayModeId id) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010055 mDisplayMode->mId = id;
56 return *this;
57 }
58
Ady Abraham5e7ee862021-06-23 17:43:41 -070059 Builder& setPhysicalDisplayId(PhysicalDisplayId id) {
60 mDisplayMode->mPhysicalDisplayId = id;
61 return *this;
62 }
63
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010064 Builder& setWidth(int32_t width) {
65 mDisplayMode->mWidth = width;
66 return *this;
67 }
68
69 Builder& setHeight(int32_t height) {
70 mDisplayMode->mHeight = height;
71 return *this;
72 }
73
74 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
Marin Shalamanov5801c942020-12-17 17:00:13 +010075 mDisplayMode->mFps = Fps::fromPeriodNsecs(vsyncPeriod);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010076 return *this;
77 }
78
79 Builder& setDpiX(int32_t dpiX) {
80 if (dpiX == -1) {
81 mDisplayMode->mDpiX = getDefaultDensity();
82 } else {
83 mDisplayMode->mDpiX = dpiX / 1000.0f;
84 }
85 return *this;
86 }
87
88 Builder& setDpiY(int32_t dpiY) {
89 if (dpiY == -1) {
90 mDisplayMode->mDpiY = getDefaultDensity();
91 } else {
92 mDisplayMode->mDpiY = dpiY / 1000.0f;
93 }
94 return *this;
95 }
96
Marin Shalamanova7fe3042021-01-29 21:02:08 +010097 Builder& setGroup(int32_t group) {
98 mDisplayMode->mGroup = group;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010099 return *this;
100 }
101
102 private:
103 float getDefaultDensity() {
104 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
105 // resolution displays get TV density. Maybe eventually we'll need to update
106 // it for 4k displays, though hopefully those will just report accurate DPI
107 // information to begin with. This is also used for virtual displays and
108 // older HWC implementations, so be careful about orientation.
109
110 auto longDimension = std::max(mDisplayMode->mWidth, mDisplayMode->mHeight);
111 if (longDimension >= 1080) {
112 return ACONFIGURATION_DENSITY_XHIGH;
113 } else {
114 return ACONFIGURATION_DENSITY_TV;
115 }
116 }
117 std::shared_ptr<DisplayMode> mDisplayMode;
118 };
119
Marin Shalamanov23c44202020-12-22 19:09:20 +0100120 DisplayModeId getId() const { return mId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100121 hal::HWConfigId getHwcId() const { return mHwcId; }
Ady Abraham5e7ee862021-06-23 17:43:41 -0700122 PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100123
124 int32_t getWidth() const { return mWidth; }
125 int32_t getHeight() const { return mHeight; }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100126 ui::Size getSize() const { return {mWidth, mHeight}; }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100127 Fps getFps() const { return mFps; }
128 nsecs_t getVsyncPeriod() const { return mFps.getPeriodNsecs(); }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100129 float getDpiX() const { return mDpiX; }
130 float getDpiY() const { return mDpiY; }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100131
132 // Switches between modes in the same group are seamless, i.e.
133 // without visual interruptions such as a black screen.
134 int32_t getGroup() const { return mGroup; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100135
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100136 bool equalsExceptDisplayModeId(const DisplayModePtr& other) const {
137 return mHwcId == other->mHwcId && mWidth == other->mWidth && mHeight == other->mHeight &&
138 getVsyncPeriod() == other->getVsyncPeriod() && mDpiX == other->mDpiX &&
139 mDpiY == other->mDpiY && mGroup == other->mGroup;
140 }
141
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100142private:
143 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
144
145 hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100146 DisplayModeId mId;
Ady Abraham5e7ee862021-06-23 17:43:41 -0700147 PhysicalDisplayId mPhysicalDisplayId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100148
149 int32_t mWidth = -1;
150 int32_t mHeight = -1;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100151 Fps mFps;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100152 float mDpiX = -1;
153 float mDpiY = -1;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100154 int32_t mGroup = -1;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100155};
156
Marin Shalamanov5801c942020-12-17 17:00:13 +0100157inline std::string to_string(const DisplayMode& mode) {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100158 return base::StringPrintf("{id=%d, hwcId=%d, width=%d, height=%d, refreshRate=%s, "
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100159 "dpiX=%.2f, dpiY=%.2f, group=%d}",
Marin Shalamanov5801c942020-12-17 17:00:13 +0100160 mode.getId().value(), mode.getHwcId(), mode.getWidth(),
161 mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpiX(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100162 mode.getDpiY(), mode.getGroup());
Marin Shalamanov5801c942020-12-17 17:00:13 +0100163}
164
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -0800165} // namespace android