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