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