blob: 1f0f3c3c33974804fa3ef50a212a234a885aa91f [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
19#include "DisplayHardware/Hal.h"
Marin Shalamanov5801c942020-12-17 17:00:13 +010020#include "Fps.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010021#include "Scheduler/StrongTyping.h"
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>
Marin Shalamanov045b7002021-01-07 16:56:24 +010025#include <ui/Size.h>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010026#include <utils/Timers.h>
27
Marin Shalamanov23c44202020-12-22 19:09:20 +010028#include <cstddef>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010029#include <memory>
30#include <vector>
31
32namespace android {
33
34namespace hal = android::hardware::graphics::composer::hal;
35
36class DisplayMode;
37using DisplayModePtr = std::shared_ptr<const DisplayMode>;
38using DisplayModes = std::vector<DisplayModePtr>;
Marin Shalamanov23c44202020-12-22 19:09:20 +010039using DisplayModeId = StrongTyping<size_t, struct DisplayModeIdTag, Compare, Hash>;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010040
41class DisplayMode {
42public:
43 class Builder {
44 public:
45 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
46
47 DisplayModePtr build() {
48 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
49 }
50
Marin Shalamanov23c44202020-12-22 19:09:20 +010051 Builder& setId(DisplayModeId id) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010052 mDisplayMode->mId = id;
53 return *this;
54 }
55
56 Builder& setWidth(int32_t width) {
57 mDisplayMode->mWidth = width;
58 return *this;
59 }
60
61 Builder& setHeight(int32_t height) {
62 mDisplayMode->mHeight = height;
63 return *this;
64 }
65
66 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
Marin Shalamanov5801c942020-12-17 17:00:13 +010067 mDisplayMode->mFps = Fps::fromPeriodNsecs(vsyncPeriod);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010068 return *this;
69 }
70
71 Builder& setDpiX(int32_t dpiX) {
72 if (dpiX == -1) {
73 mDisplayMode->mDpiX = getDefaultDensity();
74 } else {
75 mDisplayMode->mDpiX = dpiX / 1000.0f;
76 }
77 return *this;
78 }
79
80 Builder& setDpiY(int32_t dpiY) {
81 if (dpiY == -1) {
82 mDisplayMode->mDpiY = getDefaultDensity();
83 } else {
84 mDisplayMode->mDpiY = dpiY / 1000.0f;
85 }
86 return *this;
87 }
88
Marin Shalamanova7fe3042021-01-29 21:02:08 +010089 Builder& setGroup(int32_t group) {
90 mDisplayMode->mGroup = group;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010091 return *this;
92 }
93
94 private:
95 float getDefaultDensity() {
96 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
97 // resolution displays get TV density. Maybe eventually we'll need to update
98 // it for 4k displays, though hopefully those will just report accurate DPI
99 // information to begin with. This is also used for virtual displays and
100 // older HWC implementations, so be careful about orientation.
101
102 auto longDimension = std::max(mDisplayMode->mWidth, mDisplayMode->mHeight);
103 if (longDimension >= 1080) {
104 return ACONFIGURATION_DENSITY_XHIGH;
105 } else {
106 return ACONFIGURATION_DENSITY_TV;
107 }
108 }
109 std::shared_ptr<DisplayMode> mDisplayMode;
110 };
111
Marin Shalamanov23c44202020-12-22 19:09:20 +0100112 DisplayModeId getId() const { return mId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100113 hal::HWConfigId getHwcId() const { return mHwcId; }
114
115 int32_t getWidth() const { return mWidth; }
116 int32_t getHeight() const { return mHeight; }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100117 ui::Size getSize() const { return {mWidth, mHeight}; }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100118 Fps getFps() const { return mFps; }
119 nsecs_t getVsyncPeriod() const { return mFps.getPeriodNsecs(); }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100120 float getDpiX() const { return mDpiX; }
121 float getDpiY() const { return mDpiY; }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100122
123 // Switches between modes in the same group are seamless, i.e.
124 // without visual interruptions such as a black screen.
125 int32_t getGroup() const { return mGroup; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100126
127private:
128 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
129
130 hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100131 DisplayModeId mId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100132
133 int32_t mWidth = -1;
134 int32_t mHeight = -1;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100135 Fps mFps;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100136 float mDpiX = -1;
137 float mDpiY = -1;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100138 int32_t mGroup = -1;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100139};
140
Marin Shalamanov5801c942020-12-17 17:00:13 +0100141inline std::string to_string(const DisplayMode& mode) {
142 return base::StringPrintf("{id=%zu, hwcId=%d, width=%d, height=%d, refreshRate=%s, "
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100143 "dpiX=%.2f, dpiY=%.2f, group=%d}",
Marin Shalamanov5801c942020-12-17 17:00:13 +0100144 mode.getId().value(), mode.getHwcId(), mode.getWidth(),
145 mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpiX(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100146 mode.getDpiY(), mode.getGroup());
Marin Shalamanov5801c942020-12-17 17:00:13 +0100147}
148
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100149} // namespace android