blob: 31d1245ed09b90e0f6a16ab7ed5c7b099f570076 [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
89 Builder& setConfigGroup(int32_t configGroup) {
90 mDisplayMode->mConfigGroup = configGroup;
91 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; }
122 int32_t getConfigGroup() const { return mConfigGroup; }
123
124private:
125 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
126
127 hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100128 DisplayModeId mId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100129
130 int32_t mWidth = -1;
131 int32_t mHeight = -1;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100132 Fps mFps;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100133 float mDpiX = -1;
134 float mDpiY = -1;
135 int32_t mConfigGroup = -1;
136};
137
Marin Shalamanov5801c942020-12-17 17:00:13 +0100138inline std::string to_string(const DisplayMode& mode) {
139 return base::StringPrintf("{id=%zu, hwcId=%d, width=%d, height=%d, refreshRate=%s, "
140 "dpiX=%.2f, dpiY=%.2f, configGroup=%d}",
141 mode.getId().value(), mode.getHwcId(), mode.getWidth(),
142 mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpiX(),
143 mode.getDpiY(), mode.getConfigGroup());
144}
145
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100146} // namespace android