blob: 61c1b61c914b3cd0ead0faef2bbcaf745ed3b0d0 [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>
25#include <utils/Timers.h>
26
Marin Shalamanov23c44202020-12-22 19:09:20 +010027#include <cstddef>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010028#include <memory>
29#include <vector>
30
31namespace android {
32
33namespace hal = android::hardware::graphics::composer::hal;
34
35class DisplayMode;
36using DisplayModePtr = std::shared_ptr<const DisplayMode>;
37using DisplayModes = std::vector<DisplayModePtr>;
Marin Shalamanov23c44202020-12-22 19:09:20 +010038using DisplayModeId = StrongTyping<size_t, struct DisplayModeIdTag, Compare, Hash>;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010039
40class DisplayMode {
41public:
42 class Builder {
43 public:
44 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
45
46 DisplayModePtr build() {
47 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
48 }
49
Marin Shalamanov23c44202020-12-22 19:09:20 +010050 Builder& setId(DisplayModeId id) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010051 mDisplayMode->mId = id;
52 return *this;
53 }
54
55 Builder& setWidth(int32_t width) {
56 mDisplayMode->mWidth = width;
57 return *this;
58 }
59
60 Builder& setHeight(int32_t height) {
61 mDisplayMode->mHeight = height;
62 return *this;
63 }
64
65 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
Marin Shalamanov5801c942020-12-17 17:00:13 +010066 mDisplayMode->mFps = Fps::fromPeriodNsecs(vsyncPeriod);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010067 return *this;
68 }
69
70 Builder& setDpiX(int32_t dpiX) {
71 if (dpiX == -1) {
72 mDisplayMode->mDpiX = getDefaultDensity();
73 } else {
74 mDisplayMode->mDpiX = dpiX / 1000.0f;
75 }
76 return *this;
77 }
78
79 Builder& setDpiY(int32_t dpiY) {
80 if (dpiY == -1) {
81 mDisplayMode->mDpiY = getDefaultDensity();
82 } else {
83 mDisplayMode->mDpiY = dpiY / 1000.0f;
84 }
85 return *this;
86 }
87
88 Builder& setConfigGroup(int32_t configGroup) {
89 mDisplayMode->mConfigGroup = configGroup;
90 return *this;
91 }
92
93 private:
94 float getDefaultDensity() {
95 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
96 // resolution displays get TV density. Maybe eventually we'll need to update
97 // it for 4k displays, though hopefully those will just report accurate DPI
98 // information to begin with. This is also used for virtual displays and
99 // older HWC implementations, so be careful about orientation.
100
101 auto longDimension = std::max(mDisplayMode->mWidth, mDisplayMode->mHeight);
102 if (longDimension >= 1080) {
103 return ACONFIGURATION_DENSITY_XHIGH;
104 } else {
105 return ACONFIGURATION_DENSITY_TV;
106 }
107 }
108 std::shared_ptr<DisplayMode> mDisplayMode;
109 };
110
Marin Shalamanov23c44202020-12-22 19:09:20 +0100111 DisplayModeId getId() const { return mId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100112 hal::HWConfigId getHwcId() const { return mHwcId; }
113
114 int32_t getWidth() const { return mWidth; }
115 int32_t getHeight() const { return mHeight; }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100116 Fps getFps() const { return mFps; }
117 nsecs_t getVsyncPeriod() const { return mFps.getPeriodNsecs(); }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100118 float getDpiX() const { return mDpiX; }
119 float getDpiY() const { return mDpiY; }
120 int32_t getConfigGroup() const { return mConfigGroup; }
121
122private:
123 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
124
125 hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100126 DisplayModeId mId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100127
128 int32_t mWidth = -1;
129 int32_t mHeight = -1;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100130 Fps mFps;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100131 float mDpiX = -1;
132 float mDpiY = -1;
133 int32_t mConfigGroup = -1;
134};
135
Marin Shalamanov5801c942020-12-17 17:00:13 +0100136inline std::string to_string(const DisplayMode& mode) {
137 return base::StringPrintf("{id=%zu, hwcId=%d, width=%d, height=%d, refreshRate=%s, "
138 "dpiX=%.2f, dpiY=%.2f, configGroup=%d}",
139 mode.getId().value(), mode.getHwcId(), mode.getWidth(),
140 mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpiX(),
141 mode.getDpiY(), mode.getConfigGroup());
142}
143
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100144} // namespace android