blob: 69fd00e3a489531962328655c5863c305722f2a4 [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"
20#include "Scheduler/HwcStrongTypes.h"
21
22#include <android/configuration.h>
23#include <utils/Timers.h>
24
25#include <memory>
26#include <vector>
27
28namespace android {
29
30namespace hal = android::hardware::graphics::composer::hal;
31
32class DisplayMode;
33using DisplayModePtr = std::shared_ptr<const DisplayMode>;
34using DisplayModes = std::vector<DisplayModePtr>;
35
36class DisplayMode {
37public:
38 class Builder {
39 public:
40 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
41
42 DisplayModePtr build() {
43 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
44 }
45
46 Builder& setId(HwcConfigIndexType id) {
47 mDisplayMode->mId = id;
48 return *this;
49 }
50
51 Builder& setWidth(int32_t width) {
52 mDisplayMode->mWidth = width;
53 return *this;
54 }
55
56 Builder& setHeight(int32_t height) {
57 mDisplayMode->mHeight = height;
58 return *this;
59 }
60
61 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
62 mDisplayMode->mVsyncPeriod = vsyncPeriod;
63 return *this;
64 }
65
66 Builder& setDpiX(int32_t dpiX) {
67 if (dpiX == -1) {
68 mDisplayMode->mDpiX = getDefaultDensity();
69 } else {
70 mDisplayMode->mDpiX = dpiX / 1000.0f;
71 }
72 return *this;
73 }
74
75 Builder& setDpiY(int32_t dpiY) {
76 if (dpiY == -1) {
77 mDisplayMode->mDpiY = getDefaultDensity();
78 } else {
79 mDisplayMode->mDpiY = dpiY / 1000.0f;
80 }
81 return *this;
82 }
83
84 Builder& setConfigGroup(int32_t configGroup) {
85 mDisplayMode->mConfigGroup = configGroup;
86 return *this;
87 }
88
89 private:
90 float getDefaultDensity() {
91 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
92 // resolution displays get TV density. Maybe eventually we'll need to update
93 // it for 4k displays, though hopefully those will just report accurate DPI
94 // information to begin with. This is also used for virtual displays and
95 // older HWC implementations, so be careful about orientation.
96
97 auto longDimension = std::max(mDisplayMode->mWidth, mDisplayMode->mHeight);
98 if (longDimension >= 1080) {
99 return ACONFIGURATION_DENSITY_XHIGH;
100 } else {
101 return ACONFIGURATION_DENSITY_TV;
102 }
103 }
104 std::shared_ptr<DisplayMode> mDisplayMode;
105 };
106
107 HwcConfigIndexType getId() const { return mId; }
108 hal::HWConfigId getHwcId() const { return mHwcId; }
109
110 int32_t getWidth() const { return mWidth; }
111 int32_t getHeight() const { return mHeight; }
112 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
113 float getDpiX() const { return mDpiX; }
114 float getDpiY() const { return mDpiY; }
115 int32_t getConfigGroup() const { return mConfigGroup; }
116
117private:
118 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
119
120 hal::HWConfigId mHwcId;
121 HwcConfigIndexType mId;
122
123 int32_t mWidth = -1;
124 int32_t mHeight = -1;
125 nsecs_t mVsyncPeriod = -1;
126 float mDpiX = -1;
127 float mDpiY = -1;
128 int32_t mConfigGroup = -1;
129};
130
131} // namespace android