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