1 /*
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 "Fps.h"
21 #include "Scheduler/StrongTyping.h"
22 
23 #include <android-base/stringprintf.h>
24 #include <android/configuration.h>
25 #include <ui/DisplayId.h>
26 #include <ui/DisplayMode.h>
27 #include <ui/Size.h>
28 #include <utils/Timers.h>
29 
30 #include <cstddef>
31 #include <memory>
32 #include <vector>
33 
34 namespace android {
35 
36 namespace hal = android::hardware::graphics::composer::hal;
37 
38 class DisplayMode;
39 using DisplayModePtr = std::shared_ptr<const DisplayMode>;
40 using DisplayModes = std::vector<DisplayModePtr>;
41 using DisplayModeId = StrongTyping<ui::DisplayModeId, struct DisplayModeIdTag, Compare, Hash>;
42 
43 class DisplayMode {
44 public:
45     class Builder {
46     public:
Builder(hal::HWConfigId id)47         explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
48 
build()49         DisplayModePtr build() {
50             return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
51         }
52 
setId(DisplayModeId id)53         Builder& setId(DisplayModeId id) {
54             mDisplayMode->mId = id;
55             return *this;
56         }
57 
setPhysicalDisplayId(PhysicalDisplayId id)58         Builder& setPhysicalDisplayId(PhysicalDisplayId id) {
59             mDisplayMode->mPhysicalDisplayId = id;
60             return *this;
61         }
62 
setWidth(int32_t width)63         Builder& setWidth(int32_t width) {
64             mDisplayMode->mWidth = width;
65             return *this;
66         }
67 
setHeight(int32_t height)68         Builder& setHeight(int32_t height) {
69             mDisplayMode->mHeight = height;
70             return *this;
71         }
72 
setVsyncPeriod(int32_t vsyncPeriod)73         Builder& setVsyncPeriod(int32_t vsyncPeriod) {
74             mDisplayMode->mFps = Fps::fromPeriodNsecs(vsyncPeriod);
75             return *this;
76         }
77 
setDpiX(int32_t dpiX)78         Builder& setDpiX(int32_t dpiX) {
79             if (dpiX == -1) {
80                 mDisplayMode->mDpiX = getDefaultDensity();
81             } else {
82                 mDisplayMode->mDpiX = dpiX / 1000.0f;
83             }
84             return *this;
85         }
86 
setDpiY(int32_t dpiY)87         Builder& setDpiY(int32_t dpiY) {
88             if (dpiY == -1) {
89                 mDisplayMode->mDpiY = getDefaultDensity();
90             } else {
91                 mDisplayMode->mDpiY = dpiY / 1000.0f;
92             }
93             return *this;
94         }
95 
setGroup(int32_t group)96         Builder& setGroup(int32_t group) {
97             mDisplayMode->mGroup = group;
98             return *this;
99         }
100 
101     private:
getDefaultDensity()102         float getDefaultDensity() {
103             // Default density is based on TVs: 1080p displays get XHIGH density, lower-
104             // resolution displays get TV density. Maybe eventually we'll need to update
105             // it for 4k displays, though hopefully those will just report accurate DPI
106             // information to begin with. This is also used for virtual displays and
107             // older HWC implementations, so be careful about orientation.
108 
109             auto longDimension = std::max(mDisplayMode->mWidth, mDisplayMode->mHeight);
110             if (longDimension >= 1080) {
111                 return ACONFIGURATION_DENSITY_XHIGH;
112             } else {
113                 return ACONFIGURATION_DENSITY_TV;
114             }
115         }
116         std::shared_ptr<DisplayMode> mDisplayMode;
117     };
118 
getId()119     DisplayModeId getId() const { return mId; }
getHwcId()120     hal::HWConfigId getHwcId() const { return mHwcId; }
getPhysicalDisplayId()121     PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; }
122 
getWidth()123     int32_t getWidth() const { return mWidth; }
getHeight()124     int32_t getHeight() const { return mHeight; }
getSize()125     ui::Size getSize() const { return {mWidth, mHeight}; }
getFps()126     Fps getFps() const { return mFps; }
getVsyncPeriod()127     nsecs_t getVsyncPeriod() const { return mFps.getPeriodNsecs(); }
getDpiX()128     float getDpiX() const { return mDpiX; }
getDpiY()129     float getDpiY() const { return mDpiY; }
130 
131     // Switches between modes in the same group are seamless, i.e.
132     // without visual interruptions such as a black screen.
getGroup()133     int32_t getGroup() const { return mGroup; }
134 
equalsExceptDisplayModeId(const DisplayModePtr & other)135     bool equalsExceptDisplayModeId(const DisplayModePtr& other) const {
136         return mHwcId == other->mHwcId && mWidth == other->mWidth && mHeight == other->mHeight &&
137                 getVsyncPeriod() == other->getVsyncPeriod() && mDpiX == other->mDpiX &&
138                 mDpiY == other->mDpiY && mGroup == other->mGroup;
139     }
140 
141 private:
DisplayMode(hal::HWConfigId id)142     explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
143 
144     hal::HWConfigId mHwcId;
145     DisplayModeId mId;
146     PhysicalDisplayId mPhysicalDisplayId;
147 
148     int32_t mWidth = -1;
149     int32_t mHeight = -1;
150     Fps mFps;
151     float mDpiX = -1;
152     float mDpiY = -1;
153     int32_t mGroup = -1;
154 };
155 
to_string(const DisplayMode & mode)156 inline std::string to_string(const DisplayMode& mode) {
157     return base::StringPrintf("{id=%d, hwcId=%d, width=%d, height=%d, refreshRate=%s, "
158                               "dpiX=%.2f, dpiY=%.2f, group=%d}",
159                               mode.getId().value(), mode.getHwcId(), mode.getWidth(),
160                               mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpiX(),
161                               mode.getDpiY(), mode.getGroup());
162 }
163 
164 } // namespace android