Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 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" |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 20 | #include "Fps.h" |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 21 | #include "Scheduler/StrongTyping.h" |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 22 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 24 | #include <android/configuration.h> |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 25 | #include <ui/Size.h> |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 26 | #include <utils/Timers.h> |
| 27 | |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 28 | #include <cstddef> |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | namespace hal = android::hardware::graphics::composer::hal; |
| 35 | |
| 36 | class DisplayMode; |
| 37 | using DisplayModePtr = std::shared_ptr<const DisplayMode>; |
| 38 | using DisplayModes = std::vector<DisplayModePtr>; |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 39 | using DisplayModeId = StrongTyping<size_t, struct DisplayModeIdTag, Compare, Hash>; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 40 | |
| 41 | class DisplayMode { |
| 42 | public: |
| 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 Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 51 | Builder& setId(DisplayModeId id) { |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 52 | 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 Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 67 | mDisplayMode->mFps = Fps::fromPeriodNsecs(vsyncPeriod); |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 68 | 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 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 89 | Builder& setGroup(int32_t group) { |
| 90 | mDisplayMode->mGroup = group; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 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 Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 112 | DisplayModeId getId() const { return mId; } |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 113 | hal::HWConfigId getHwcId() const { return mHwcId; } |
| 114 | |
| 115 | int32_t getWidth() const { return mWidth; } |
| 116 | int32_t getHeight() const { return mHeight; } |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 117 | ui::Size getSize() const { return {mWidth, mHeight}; } |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 118 | Fps getFps() const { return mFps; } |
| 119 | nsecs_t getVsyncPeriod() const { return mFps.getPeriodNsecs(); } |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 120 | float getDpiX() const { return mDpiX; } |
| 121 | float getDpiY() const { return mDpiY; } |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 122 | |
| 123 | // Switches between modes in the same group are seamless, i.e. |
| 124 | // without visual interruptions such as a black screen. |
| 125 | int32_t getGroup() const { return mGroup; } |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 126 | |
| 127 | private: |
| 128 | explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {} |
| 129 | |
| 130 | hal::HWConfigId mHwcId; |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 131 | DisplayModeId mId; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 132 | |
| 133 | int32_t mWidth = -1; |
| 134 | int32_t mHeight = -1; |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 135 | Fps mFps; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 136 | float mDpiX = -1; |
| 137 | float mDpiY = -1; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 138 | int32_t mGroup = -1; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 141 | inline std::string to_string(const DisplayMode& mode) { |
| 142 | return base::StringPrintf("{id=%zu, hwcId=%d, width=%d, height=%d, refreshRate=%s, " |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 143 | "dpiX=%.2f, dpiY=%.2f, group=%d}", |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 144 | mode.getId().value(), mode.getHwcId(), mode.getWidth(), |
| 145 | mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpiX(), |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 146 | mode.getDpiY(), mode.getGroup()); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 149 | } // namespace android |